我有一個這樣的模型:貓鼬骨料獲得成員總數在一組模型
var field = {
createdAt: {type: Date, default: Date.now()},
updatedAt: {type: Date, default: Date.now()},
pic : {type: String, default: 'http://lorempixel.com/400/400/abstract/', writable: true},
thumb : {type: String, default: 'http://lorempixel.com/100/100/abstract/', writable: true},
name: {type: String},
description: {type: String},
isPublic: {type: Boolean, default: true},
members: [{type: Schema.Types.ObjectId, ref: 'Member'}],
}
im使用低於此代碼即可獲得Member
的ID的成員中字段的總數。
Group.aggregate([
{$match: {_id:req.params.group_id}},
{$group: {
_id: '$members',
count: {$sum: 1}
}}
], function (err, count) {
res.send(count);
});
但它返回併爲空數組[]
如何獲得正確的計數?
謝謝。
貓鼬不「自動施放」爲'ObjectId'聚集管道。你需要自己施放它,否則你的'$ match'什麼都不匹配。之前被問過,讓我搜索重複(s)。 –
可能重複的[無法使用貓匹配貓鼬和聚合框架](http://stackoverflow.com/questions/14551387/cant-use-match-with-mongoose-and-the-aggregation-framework) –
同樣在[問題#1399](https://github.com/Automattic/mongoose/issues/1399) –