【发布时间】:2017-01-16 22:22:16
【问题描述】:
WhiskeySchema.statics.count = function (data){
var distillerIds = data.map(function (a) {
return a.distiller._id;
});
return Whiskey
.aggregate([
{ $group: {
// Group by fields to match on distiller
_id: { distiller: '$distiller'},
// Count number of matching whiskeys for the group
count: { $sum: 1 }
}},
{
$match: {
distiller : {
$in: distillerIds
}
}
}
])
.execAsync()
.then(function(countedData){
console.log('counted data :', countedData)
return countedData;
})
.catch(function(err){
console.log('whiskey count err : ', err);
})
};
distiller / $distiller 是一个 mongo ObjectID。一个参考。
我想通过特定的 ID 来查找,就像我在比赛中所做的那样。
然后,在这些匹配的结果中,当威士忌中的蒸馏器 ID 相同时,我想对它们进行分组,并对它们进行计数。 (试图计算每个蒸馏器的威士忌总数)。
匹配按预期工作,但是当我包含组和计数时,我不断收到为 countedData 返回的空数组。
counted data : []
【问题讨论】:
标签: mongodb mongoose aggregation-framework