企业🤖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" } ]); ```