0
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 : []