Reputation:
I'm new to MongoDB and Mongoose.
Is there any simple way to get count of answers for each topic after population method?
var query = Topic.find({});
query.select("title answersRef");
query.populate({path:"answersRef", select:"_id"}); //here I want count of answers
query.exec(function(topicError, topicResult){
res.render('topic', { topic: topicResult });
});
On a webpage, I would like to show every topic's title that I find from database with the number of comments on each topic.
Upvotes: 1
Views: 2123
Reputation: 12019
The answersRef
property of the topicResult
contains all the answers so you can just obtain the length:
query.exec(function(topicError, topicResult){
var answerCount = topicResult.answerRef.length;
res.render('topic', { topic: topicResult });
});
Upvotes: 1