2013-05-03 123 views
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); } 
}); 

我怎么能算一些文档中的注释?

回答

1

你可以做到这一点.length

Model.findById(ObjectId, function(err, res) { 
    if (err) { throw err; } 
    if (!res) { console.log('model not found'); } 
    console.log(res.comments.length); 
};