💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
``` 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" } ]); ```