【发布时间】:2018-02-03 06:23:20
【问题描述】:
我有一个预订模式,其中预订由多个客户完成。
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,并为所有这些 clientId 执行聚合,并在一个查询中获取聚合结果。现在我正在执行一个循环操作来获取每个的结果客户。
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);
})
}
那么有没有更好的方法来做到这一点,而无需遍历 clientsID 并将其传递给我的 mongoose 函数。
【问题讨论】:
标签: javascript node.js mongodb mongoose aggregation-framework