Mongo的分組操作有兩種方式:aggregate({$group:{}})和group()
1.db.collection.aggregate([$group{}])
{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }
_id
為必選字段小压,為被分組字段泉蝌,可為空或null
<accumulator>
為可選字段吧兔,其中可包含一下運算符:
示例
1.僅分組,對issue_xjtf表中sp1航厚,sp2進行分組
db.getCollection("issue_xjtf").aggregate({$group:{
_id:{vol:"$sp1",issue:"$sp2"}}})
相當于sql
Select sp1 as vol,sp2 as issue
From issue_xjtf
group by vol,issue
2.運算符應(yīng)用 :first,sum,max
db.getCollection("issue_xjtf").aggregate({
$group:{
_id:{vol:"$sp1",issue:"$sp2"},
first_url:{$first:"$url"}, #first為獲取第一個昌渤,last為獲取最后一個
count:{$sum:1}, #若求某字段和演侯,需將1改成“$”+字段名的形式,avg可用于獲取字段平均值
time:{$max:"$inserttime"} #max獲取最大日期伐憾,min可獲取最小日期
}})
2.db.collection.group()
db.collection.group({ key, reduce, initial [, keyf] [, cond] [, finalize] })
前三個是必備參數(shù)勉痴,“[]”中是可選參數(shù)
key
可以放用來分組的字段,并且會返回其中字段(group by 后面的字段)
reduce
是在分組操作期間對文檔進行操作的聚合函數(shù)树肃≌裘可以返回總和或計數(shù)。該函數(shù)有兩個參數(shù):當前文檔;該組的聚合結(jié)果文檔雏掠。
initial
對結(jié)果中文檔斩祭,字段進行初始化
cond
對數(shù)據(jù)篩選的條件,相當于where
示例:
1.count:取xbgi表中乡话,article_pubdate值大于2000-01-01的數(shù)據(jù)摧玫,并分組計數(shù)
db.getCollection("xbgi").group({
key: {'article_pubdate': 1},
cond: { article_pubdate: { $gt: '2000-01-01' } },
reduce: function(obj,article_pubdate) {article_pubdate.count++},
initial: { count:0}
})
相當于sql
select article_pubdate,count(article_pubdate)
from xbgi
where article_pubdate >'2000-01-01'
group by article_pubdate;
2.max:取sjwd表中,ric_publication_coden為9529a8f7-3eef-431a-a0cd-e49d601417df绑青,用article_year分組計數(shù)诬像,取其最晚日期。
db.getCollection("sjwd").group(
{
key: {article_year:1},
cond: { ric_publication_coden: '9529a8f7-3eef-431a-a0cd-e49d601417df' },
reduce: function( curr, result ) {//curr, result為自定義參數(shù)
result.count ++;
if(result.itime<curr.odi_posttime){
result.itime=curr.odi_posttime;}
},
initial: { count : 0,'itime':'0'}
}
)
相當于sql
Select article_year,count(*) as count,max(odi_posttime) as itime
From sjwd
Where ric_publication_coden= '9529a8f7-3eef-431a-a0cd-e49d601417df'
Group By article_year;
3.sum:在表total_journal_issue中以journal_id分組闸婴,并獲取article_count總數(shù)
db.getCollection("total_journal_issue").group({
key: {journal_id:1},
reduce: function(curr,result){//curr當前文檔, result結(jié)果文檔
if(typeof curr.article_count!="undefined"){//判斷article_count字段是否存在,其他方式參見js語法
result.sum_article=curr.article_count+result.sum_article;
}},
initial: {sum_article:0}//sum_article字段初始化
})
相當于sql
Select journal_id,sum(article_count) as sum_article
From total_journal_issue
Group By journal_id
注:分組限制
用Navicat 執(zhí)行g(shù)roup()時,分組值超過20000固灵,會報如下錯誤(未檢測具體原因):
Error: errmsg: "group() can't handle more than 20000 unique keys"