0

我需要计算主文档的子文档。只是一个方案的例子:

模型:

{
    title: {type: String, required: true},
    content: {type: String, required: true},
    comments: [{
        comment: {type: String, required: true},
        author: {type: String, required: true}
    }]
}

此查询不起作用:

Model.findById(ObjectId).count('comments', function(err, res) { 
    if (err) { throw err; } 
    else { console.log(res); } 
});

如何计算某些文档中的评论?

4

1 回答 1

2

你可以这样做.length

Model.findById(ObjectId, function(err, res) {
    if (err) { throw err; }
    if (!res) { console.log('model not found'); }
    console.log(res.comments.length);
};
于 2013-05-04T09:37:17.340 回答