先认识几个关键字
aggregate
group
reduce
merge
1、根据description字段进行分组,只返回分组字段值
db.$collectionName.aggregate({"$group":{"_id":"$description"}}):等价于select description _id from t group by description
其中,group是分组关键字,必须用$修饰。分组字段必须用_id指定,用其他的会报错。分组字段也必须用$修饰。
2、根据description字段进行分组,返回分组字段值
db.$collectionName.aggregate({"$group":{"_id":"$description", "cc":{"$sum":1}}}):等价于select description _id, count(1) cc from t group by description
db.$collectionName.aggregate({"$group":{"_id":"$description", "avg_score":{"$avg":"$score"}}}):等价于select description _id, avg(score) avg_score from t group by description
标签:第三篇,聚合,description,mongodb,score,分组,group,avg,id From: https://www.cnblogs.com/koushr/p/16708581.html