我很难在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分组?
发布于 2014-12-05 21:08:49
使用$match而不是$where,并将每个管道操作放到自己的对象中:
Room.aggregate([
{ "$match": { "roomId" : myIdHere } },
{ "$group": {
"_id": '$mobileUser.genderType',
"genderTypeCount": {
"$sum": 1
}
}}
], function(err, result) {...})https://stackoverflow.com/questions/27324613
复制相似问题