0
我有两个集合:问题和答案。每个答案都属于一个问题,所以答案有它的“question_id”。Mongoosejs在for循环中使用诺言
我想获得拥有最回答五个问题:
Answer.aggregate([
{
$group: {
_id: '$question_id',
num_questions: { $sum: 1 },
}
},
{
$sort: {
'num_questions': -1
}
}
]).then(function(answers) {
var result = [];
for(answer of answers) {
Question.findById(answer._id, function(err, question) {
result.push(question);
});
}
console.log(result);
res.json({questions: result});
});
,但与上面的代码,我的结果将是,因为异步机制无效。
我该怎么做?
另一个问题,我有聚合查询,我只能有问题,至少有一个答案。我怎样才能得到没有答案的问题?
您的请求只返回一个问题。我怎么能得到五个?谢谢 ! –
@谭林峰编辑5 –
非常感谢。你能帮我解答我的第二个问题吗?关于一些没有任何答案的问题。我如何显示它们? –