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

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

軟件下載吧

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

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

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

MongoDB 聚合

MongoDB中聚合(aggregate)主要用于處理數(shù)據(jù)(諸如統(tǒng)計(jì)平均值,求和等),并返回計(jì)算后的數(shù)據(jù)結(jié)果。有點(diǎn)類似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計(jì)算總和。

  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 計(jì)算平均值

  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)計(jì)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)計(jì)出每個country/province下的userid的數(shù)量(同一個userid只統(tǒng)計(jì)一次)

  過程如下。

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

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

  結(jié)果是錯誤的:

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

  原因是,這樣來統(tǒng)計(jì)不能區(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)計(jì):

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

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

相關(guān)下載

查看所有評論+

網(wǎng)友評論

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

熱門閱覽

最新排行

公眾號

主站蜘蛛池模板: 成人亚洲精品 | 亚洲欧洲久久久精品 | 男人在线网址 | 日韩精品一区二区三区乱码 | 日韩视频网 | 1024国产欧美日韩精品 | 亚洲国产精品91 | 国产成人麻豆精品 | 国产大片中文字幕在线观看 | 日韩精品一区二区三区高清 | 久久久久久中文字幕 | 成年女人毛片免费视频永久vip | 国产精品91在线 | 欧美日韩一区二区三区在线观看 | 手机看片日韩高清国产欧美 | 全国最大色成免费网站 | 国模午夜写真福利视频在线 | 久久国内精品自在自线软件 | 免费看又黄又爽又猛的网站 | 国产三级精品久久三级国专区 | 精品国产自在在线在线观看 | 一本色道久久爱88av | 亚洲欧美午夜 | 亚洲天堂网在线观看 | 国产在线精品一区二区夜色 | 一区视频在线播放 | 模特视频一二三区 | 亚洲成人中文字幕 | 亚洲炮网| 欧美亚洲国产成人高清在线 | 成人免费a视频 | 日韩一级不卡 | 欧美日a| 国产在线极品 | 欧美大片一区 | 国产福利不卡一区二区三区 | 国产一区二区三区国产精品 | 天堂色视频 | a级毛片免费观看在线播放 a级毛片免费看 | 成年人毛片| 欧美亚洲另类视频 |