arrays - 我如何在 Mongoose 中查询以获得 "answers"数组,其中 'objective'数组内的 'answers'是 "Awareness"

标签 arrays node.js json mongoose

我正在尝试使用nodeJS中的mongoose从“响应”mongoDB获取“答案”列表,其中“目标”数组也包含“意识”和特定的“projectId”。但它返回所有列表。

Response.find({projectId: req.params.projectId,"answers.objectives": "Awareness"})
    .populate()
    .then(survey => {
        res.json(survey);
    }));

我尝试了 mongoose find 关键字,但它不起作用 这是 mongoDB 结构:
This is mongoDB Structure

最佳答案

对对象的嵌套数组使用位置运算符 $ 以仅获取匹配的元素。见下文:

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/

相关文章:

java - 如何仅使用 Scanner 读取文件并将每个句子存储在 arrayList 中?

javascript - AES 解密不适用于 crypto.js

python - 保存数据时保留新行

c - 如何在C中对结构体指针数组进行快速排序?

ios - 将 Swift 字符串转换为数组

angularjs - PhantomJS 1.9.8(Windows 7 0.0.0)错误引用错误: Can't find variable: angular

javascript - 我想在 menuData.json 中显示 Products.MenuName

android - 如何从 json 数组中检索数据

mysql - forEach 循环不返回数组

javascript - Node.js - 一项一项地执行函数