我在mongodb上进行筛选和分组时遇到麻烦。我真的不理解它是如何工作的。
例如,在此查询中:
Room.aggregate(
[{
"$where": { "roomId" : myIdHere },
"$group": {
"_id": '$mobileUser.genderType',
"genderTypeCount": {
"$sum": 1
}
}
}]
房间型号:
var roomModelSchema = mongoose.Schema({
roomId: String,
mobileUser: {
facebookId: String,
email: String,
name: String,
photoUrl: String,
genderType: String,
birthday: Date,
city: String
},
insertDate: Date
})
我应该怎么做才能按roomId过滤并按genderType分组?
使用$match
代替$where
,然后将每个管道操作放入其自己的对象中:
Room.aggregate([
{ "$match": { "roomId" : myIdHere } },
{ "$group": {
"_id": '$mobileUser.genderType',
"genderTypeCount": {
"$sum": 1
}
}}
], function(err, result) {...})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句