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