我如何获得Mongoose模型中的人口数量?
我是MongoDB和Mongoose的新手。
人口方法后,有没有简单的方法可以得到每个主题的答案?
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 }); });
在网页上,我想显示每个主题的标题,我从数据库中find每个主题的评论数量。
answersRef
属性包含所有的答案,所以你可以只获得长度:
query.exec(function(topicError, topicResult){ var answerCount = topicResult.answerRef.length; res.render('topic', { topic: topicResult }); });