mongodb 聚合
mongodb中聚合(aggregate)主要用于處理資料(諸如統計平均值,求和等),并傳回計算後的資料結果。有點類似sql語句中的 count(*)。
基本文法為:db.collection.aggregate( [ <stage1>, <stage2>, ... ] )
現在在mycol集合中有以下資料:
{ "_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(*) from mycol group by sex
mongodb: db.mycol.aggregate([{$group: {_id: '$sex', personcount: {$sum: 1}}}])
sql: select sex,sum(score) totalscore from mycol group by sex
mongodb: db.mycol.aggregate([{$group: {_id: '$sex', totalscore: {$sum: '$score'}}}])
2、$avg 計算平均值
sql: select sex,avg(score) avgscore from mycol group by sex
mongodb: db.mycol.aggregate([{$group: {_id: '$sex', avgscore: {$avg: '$score'}}}])
3、$max 擷取集合中所有文檔對應值得最大值。
sql: select sex,max(score) maxscore from mycol group by sex
mongodb: db.mycol.aggregate([{$group: {_id: '$sex', maxscore : {$max: '$score'}}}])
4、$min 擷取集合中所有文檔對應值得最小值。
sql: select sex,min(score) minscore from mycol group by sex
mongodb: db.mycol.aggregate([{$group: {_id: '$sex', minscore : {$min: '$score'}}}])
5、$push 把文檔中某一列對應的所有資料插入值到一個數組中。
mongodb: db.mycol.aggregate([{$group: {_id: '$sex', scores : {$push: '$score'}}}])
6、$addtoset 把文檔中某一列對應的所有資料插入值到一個數組中,去掉重複的
db.mycol.aggregate([{$group: {_id: '$sex', scores : {$addtoset: '$score'}}}])
7、 $first 根據資源文檔的排序擷取第一個文檔資料。
db.mycol.aggregate([{$group: {_id: '$sex', firstperson : {$first: '$name'}}}])
8、 $last 根據資源文檔的排序擷取最後一個文檔資料。
db.mycol.aggregate([{$group: {_id: '$sex', lastperson : {$last: '$name'}}}])
9、全部統計 null
db.mycol.aggregate([{$group:{_id:null,totalscore:{$push:'$score'}}}])
例子
現在在t2集合中有以下資料:
{ "country" : "china", "province" : "sh", "userid" : "a" }
{ "country" : "china", "province" : "sh", "userid" : "b" }
{ "country" : "china", "province" : "sh", "userid" : "c" }
{ "country" : "china", "province" : "bj", "userid" : "da" }
{ "country" : "china", "province" : "bj", "userid" : "fa" }
需求是統計出每個country/province下的userid的數量(同一個userid隻統計一次)
過程如下。
首先試着這樣來統計:
db.t2.aggregate([ { $group: {"_id": { "country" : "$country", "prov": "$province"} , "number":{$sum:1}} } ])
結果是錯誤的:
原因是,這樣來統計不能區分userid相同的情況 (上面的資料中sh有兩個 userid = a)
為了解決這個問題,首先執行一個group,其id 是 country, province, userid三個field:
db.t2.aggregate([ { $group: {"_id": { "country" : "$country", "province": "$province" , "uid" : "$userid" } } } ])
可以看出,這步的目的是把相同的userid隻剩下一個。
然後第二步,再第一步的結果之上再執行統計:
db.t2.aggregate([
{ $group: {"_id": { "country" : "$country", "province": "$province" , "uid" : "$userid" } } } ,
{ $group: {"_id": { "country" : "$_id.country", "province": "$_id.province" }, count : { $sum : 1 } } }
])
這回就對了
加入一個$project操作符,把_id去掉
db.t2.aggregate([ { $group: {"_id": { "country" : "$country", "province": "$province" , "uid" : "$userid" } } } ,
{ $group: {"_id": { "country" : "$_id.country", "province": "$_id.province" }, count: { $sum : 1 } } },
{ $project : {"_id": 0, "country" : "$_id.country", "province" : "$_id.province", "count" : 1}}
最終結果如下:
管道在unix和linux中一般用于将目前指令的輸出結果作為下一個指令的參數。
mongodb的聚合管道将mongodb文檔在一個管道處理完畢後将結果傳遞給下一個管道處理。管道操作是可以重複的。
表達式:處理輸入文檔并輸出。表達式是無狀态的,隻能用于計算目前聚合管道的文檔,不能處理其它的文檔。
這裡我們介紹一下聚合架構中常用的幾個操作:
$project:修改輸入文檔的結構。可以用來重命名、增加或删除域,也可以用于建立計算結果以及嵌套文檔。
$match:用于過濾資料,隻輸出符合條件的文檔。$match使用mongodb的标準查詢操作。
$limit:用來限制mongodb聚合管道傳回的文檔數。
$skip:在聚合管道中跳過指定數量的文檔,并傳回餘下的文檔。
$unwind:将文檔中的某一個數組類型字段拆分成多條,每條包含數組中的一個值。
$group:将集合中的文檔分組,可用于統計結果。
$sort:将輸入文檔排序後輸出。
$geonear:輸出接近某一地理位置的有序文檔。
1、$project執行個體
db.mycol.aggregate({$project:{name : 1, score : 1}})
這樣的話結果中就隻還有_id,name和score三個字段了,預設情況下_id字段是被包含的,如果要想不包含_id話可以這樣:
db.mycol.aggregate({$project:{_id : 0, name : 1, score : 1}})
2、$match執行個體
$match用于擷取分數大于30小于并且小于100的記錄,然後将符合條件的記錄送到下一階段$group管道操作符進行處理
db.mycol.aggregate([{$match :{score: {$gt: 30, $lt: 100}}},{$group:{_id:'$sex',count:{$sum:1}}}])