2013-05-03 8 views
0

Мне нужно подсчитать субдокументы основного документа. Просто пример схемы:Mongoose - Поддокументы подсчета

Модель:

{ 
    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); 
}; 
Смежные вопросы