ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` db.users.aggregate([ { $match:{ age:{$exists:true} } } ,{ $group:{ _id:null //被匹配的任何记录一起参与同一个分组 ,totalAge:{ $sum:"$age" } ,avg:{ $avg:"$age" } } } ]); ``` ``` db.users.aggregate([ { $match:{ age:{$exists:true} ,name:{$exists:true} } } ,{ $group:{ _id:"$name" //被匹配的记录按照name来进行分组 ,totalAge:{ $sum:"$age" } ,avg:{ $avg:"$age" } } } ]); ``` 如果hobbies是个数组,将会把一条记录分拆成n条 ``` db.users.aggregate([ { $match:{ hobbies:{$exists:true} } } ,{ $unwind:"$hobbies" } ]); ```