【问题标题】:How i can query in mongoose to get the "answers" array where 'objective' inside the 'answers' array is "Awareness"我如何在猫鼬中查询以获得“答案”数组,其中“答案”数组中的“目标”是“意识”
【发布时间】:2019-10-09 19:01:46
【问题描述】:

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

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

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

【问题讨论】:

标签: arrays node.js json mongoose


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2021-12-29
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多