0
我有一个预订模式,其中预订由多个客户端完成。通过匹配多个Id值的相同字段来合计查询
var booking = new Schema({
booking: {
start_time : { type: Date },
end_time : { type: Date },
park_location_id: { type: Schema.Types.ObjectId },
clientId: { type: Schema.Types.ObjectId },
}
});
在这里,我要寻找一个方式,我可以通过多种的clientId在数组中,并为所有这些进行汇总,并得到汇总结果在一个单一的query.Right我现在执行环路操作,获取每个客户端的结果。
booking.statics.totalCheckinReport = function(id,callback){
// Here id is single client
// can I pass an array of clientID as [ id1, id2,id3]
// and get the aggregated result of all of them
this.aggregate([
{
$match:
{
$and:[
{'booking.park_location_id' : mongoose.Types.ObjectId(id)}
]
}
},
{
$group :{
_id : id,
totalCheckinCount: { $sum : 1 },
}
}
]).exec(function(err, data){
if(err)
callback(null,err);
else
callback(null, data);
})
}
那么有没有更好的方式来做到这一点,而不循环clientID并将它传递给我的猫鼬函数。