例如查询一个用户集合中,性别为女,统计相同年龄的数量
db.User.group({
key: {
'Age' : 1
},
cond: {"sex":1},
reduce: function ( curr, result ) {
result.total += 1;
},
initial: {
total : 0
}
}
)
等同于sql中
select COUNT(0) as total,Age from User where Sex = 1 GROUP BY Age
这位简单示例 更多资料查看文档
https://docs.mongodb.com/manual/reference/method/db.collection.group/#db.collection.group