Mongodb聚合函数查询
1.match函数
db.person.aggregate([{"$match":{"address":"龙泉"}}])
2.group分组查询
db.person.aggregate([
{$group:
{_id:"$address",
count:{$sum:1}
}
}
])
3.按条件分组查询
db.person.aggregate([
{$match:
{sex:"女"}
},
{$group:
{_id:"$team",
count:{$sum:1}
}
}
])
4.平均数
db.person.aggregate([{$group : {_id : "$team", avg_age : {$avg : "$age"}}}])
5.最大数
db.person.aggregate([{$match:{sex:"女"}},{$group : {_id : "$team", max_age : {$max : "$age"}}}])
6.最小数
db.person.aggregate([{$match:{sex:"女"}},{$group : {_id : "$team", min_age : {$min : "$age"}}}])
7.求和
db.person.aggregate([{$match:{sex:"女"}},{$group : {_id : "$team", sum_age : {$sum : "$age"}}}])
8.查看第一个
db.person.aggregate([{$match:{sex:"女"}},{$group : {_id : "$team", first_address : {$first : "$address"}}}])
9.查看最后一个
db.person.aggregate([{$match:{sex:"女"}},{$group : {_id : "$team", last_address : {$last : "$address"}}}])
10.求和排序
db.person.aggregate([{$group :{_id : "$address", count:{$sum:1}}},{$sort:{count:-1}}])
11.限制显示多少行
db.person.aggregate([{$group :{_id : "$address", count:{$sum:1}}},{$sort:{count:-1}},{$limit:2}])
12.跳过多少行
db.person.aggregate([{$group :{_id : "$address", count:{$sum:1}}},{$sort:{count:1}},{$skip:1}])
标签:聚合,address,db,person,文档,mongodb5,aggregate,group,id From: https://www.cnblogs.com/shaohuang/p/17542964.html