我正在尝试使用nodeJS中的mongoose从“响应”mongoDB获取“答案”列表,其中“目标”数组也包含“意识”和特定的“projectId”。但它返回所有列表。
Response.find({projectId: req.params.projectId,"answers.objectives": "Awareness"})
.populate()
.then(survey => {
res.json(survey);
}));
最佳答案
对对象的嵌套数组使用位置运算符 $
以仅获取匹配的元素。见下文:
Response.find({projectId: req.params.projectId,"answers.objectives":
"Awareness"},{"answers.objectives.$": 1})
.populate()
.then(survey => {
res.json(survey);
}));
关于arrays - 我如何在 Mongoose 中查询以获得 "answers"数组,其中 'objective'数组内的 'answers'是 "Awareness",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56271235/