日韩小视频-日韩久久一区二区三区-日韩久久一级毛片-日韩久久久精品中文字幕-国产精品亚洲精品影院-国产精品亚洲欧美云霸高清

下載吧 - 綠色安全的游戲和軟件下載中心

軟件下載吧

當前位置:軟件下載吧 > 數(shù)據(jù)庫 > DB2 > MongoDB 數(shù)據(jù)庫基礎(chǔ) 之 聚合group的操作指南

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之 聚合group的操作指南

時間:2024-02-05 12:47作者:下載吧人氣:19

MongoDB 聚合

MongoDB中聚合(aggregate)主要用于處理數(shù)據(jù)(諸如統(tǒng)計平均值,求和等),并返回計算后的數(shù)據(jù)結(jié)果。有點類似sql語句中的 count(*)。

基本語法為:db.collection.aggregate( [ <stage1>, <stage2>, ... ] )

現(xiàn)在在mycol集合中有以下數(shù)據(jù):

{ “_id” : 1, “name” : “tom”, “sex” : “男”, “score” : 100, “age” : 34 }
{ “_id” : 2, “name” : “jeke”, “sex” : “男”, “score” : 90, “age” : 24 }
{ “_id” : 3, “name” : “kite”, “sex” : “女”, “score” : 40, “age” : 36 }
{ “_id” : 4, “name” : “herry”, “sex” : “男”, “score” : 90, “age” : 56 }
{ “_id” : 5, “name” : “marry”, “sex” : “女”, “score” : 70, “age” : 18 }
{ “_id” : 6, “name” : “john”, “sex” : “男”, “score” : 100, “age” : 31 }

1、$sum計算總和。

  Sql: select sex,count(*) frommycol group by sex

  MongoDb: db.mycol.aggregate([{$group: {_id: '$sex', personCount: {$sum: 1}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

  Sql: select sex,sum(score) totalScore frommycol group by sex

  MongoDb: db.mycol.aggregate([{$group: {_id: '$sex', totalScore: {$sum: '$score'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

2、$avg 計算平均值

  Sql: select sex,avg(score) avgScore frommycol group by sex

  Mongodb: db.mycol.aggregate([{$group: {_id: '$sex', avgScore: {$avg: '$score'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

3、$max獲取集合中所有文檔對應(yīng)值得最大值。

  Sql: select sex,max(score) maxScore frommycol group by sex

  Mongodb: db.mycol.aggregate([{$group: {_id: '$sex', maxScore: {$max: '$score'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

4、$min 獲取集合中所有文檔對應(yīng)值得最小值。

  Sql: select sex,min(score) minScore frommycol group by sex

  Mongodb: db.mycol.aggregate([{$group: {_id: '$sex', minScore: {$min: '$score'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

5、$push 把文檔中某一列對應(yīng)的所有數(shù)據(jù)插入值到一個數(shù)組中。

  Mongodb: db.mycol.aggregate([{$group: {_id: '$sex', scores : {$push: '$score'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

6、$addToSet把文檔中某一列對應(yīng)的所有數(shù)據(jù)插入值到一個數(shù)組中,去掉重復(fù)的

  db.mycol.aggregate([{$group: {_id: '$sex', scores : {$addToSet: '$score'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

7、 $first根據(jù)資源文檔的排序獲取第一個文檔數(shù)據(jù)。

  db.mycol.aggregate([{$group: {_id: '$sex', firstPerson : {$first: '$name'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

8、 $last根據(jù)資源文檔的排序獲取最后一個文檔數(shù)據(jù)。

  db.mycol.aggregate([{$group: {_id: '$sex', lastPerson : {$last: '$name'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

9、全部統(tǒng)計null

  db.mycol.aggregate([{$group:{_id:null,totalScore:{$push:'$score'}}}])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

例子

  現(xiàn)在在t2集合中有以下數(shù)據(jù):

  { “country” : “china”, “province” : “sh”, “userid” : “a” }
  { “country” : “china”, “province” : “sh”, “userid” : “b” }
  { “country” : “china”, “province” : “sh”, “userid” : “a” }
  { “country” : “china”, “province” : “sh”, “userid” : “c” }
  { “country” : “china”, “province” : “bj”, “userid” : “da” }
  { “country” : “china”, “province” : “bj”, “userid” : “fa” }

  需求是統(tǒng)計出每個country/province下的userid的數(shù)量(同一個userid只統(tǒng)計一次)

  過程如下。

  首先試著這樣來統(tǒng)計:

  db.t2.aggregate([{$group:{"_id":{"country":"$country","prov":"$province"},"number":{$sum:1}}}])

  結(jié)果是錯誤的:

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

  原因是,這樣來統(tǒng)計不能區(qū)分userid相同的情況 (上面的數(shù)據(jù)中sh有兩個 userid = a)

  為了解決這個問題,首先執(zhí)行一個group,其id 是 country, province, userid三個field:

  db.t2.aggregate([ { $group: {"_id": { "country" : "$country", "province": "$province" , "uid" : "$userid" } } } ])

MongoDB 數(shù)據(jù)庫基礎(chǔ) 之  聚合group的操作指南

  可以看出,這步的目的是把相同的userid只剩下一個。

  然后第二步,再第一步的結(jié)果之上再執(zhí)行統(tǒng)計:

  db.t2.aggregate([
  { $group: {“_id”: { “country” : “$country”, “province”: “$province” , “uid” : “$userid” } } } ,
  { $group: {“_id”: { “country” : “$_id.country”, “province”: “$_id.province” }, count : { $sum : 1 } } }
  ])

標簽MongoDB,技術(shù)文檔,數(shù)據(jù)庫,MongoDB

相關(guān)下載

查看所有評論+

網(wǎng)友評論

網(wǎng)友
您的評論需要經(jīng)過審核才能顯示

熱門閱覽

最新排行

公眾號

主站蜘蛛池模板: 另类视频一区 | 国产99视频精品一区 | 久久久精品免费观看 | 9cao在线精品免费 | 亚洲性网 | 国产91久久精品 | 三级网站免费 | 美女视频免费黄 | 玖玖精品在线观看 | 欧美一级成人免费大片 | 国产三级精品在线观看 | 久久成人国产精品免费 | 成人午夜看片在线观看 | 日韩欧美亚洲每的更新在线 | 欧美一级毛片欧美一级无片 | 在线免费观看一区二区三区 | 清纯偷拍精品视频在线观看 | 国产精品2020观看久久 | 免费一级a毛片在线播放 | 国产精品免费久久 | 国产高清免费影视在线观看 | 中文字幕一区二区三区精品 | 国产精品视频一区二区猎奇 | 2021国内自拍 | 久视频在线| 在线男人的天堂 | 欧美精品亚洲精品日韩专区 | 怡红院免费在线视频 | 免费精品一区二区三区在线观看 | 直接在线观看的三级网址 | 99精品久久久久久久免费看蜜月 | 亚洲欧美一二三区 | 午夜亚洲精品 | 亚洲成a人片在线播放 | 夜夜春夜夜夜夜猛噜噜噜噜噜 | 国产精品爱久久久久久久三级 | 午夜日本一区二区三区 | 一级毛片免费在线 | 成人国产三级在线播放 | 美女操男人 | 日本激情视频在线观看 |