如何使用Mongoose给定另一个嵌套字段值来更新嵌套字段?

时间:2020-11-01 04:03:26

标签: javascript node.js express mongoose body-parser

我正在创建一个端点,用于更新以前已经回答过的问题的答案,但是我在访问Mongoose模式中的该字段时遇到了麻烦。

我正在使用的架构响应此JSON示例:

{
    "pollId":15,
    "userId":13,
    "name":"poll name",
    "description":"poll desc.",
    "status":"poll status",
    "created":"2020-10-13",
    "modified":"2020-10-16",
    "sections":[
        {
            "sectionIndex":1,
            "sectionTitle":"section title",
            "sectionDescription":"section desc.",
            "questions":[
                {
                    "questionIndex":1,
                    "questionTitle":"quest. title ",
                    "questionType":"quest. type",
                    "value":"quest. answer", //<- This could be the field to update
                    "mandatory":true,
                    "multiline":false,
                    "adornment":"",
                    "restrictions":{"min":0,"max":10},
                    "options":[{"option":"option 1"},{"option":"option 2"},{"option":"option 3"}]
                },
                {
                    "questionIndex":2,
                    "questionTitle":"quest. title ",
                    "questionType":"quest. type",
                    "value":"quest. answer", //<- This could be the field to update
                    "mandatory":true,
                    "multiline":false,
                    "adornment":"",
                    "restrictions":{"min":0,"max":10},
                    "options":[{"option":"option 1"},{"option":"option 2"},{"option":"option 3"}]
                }]
        },
        {
            "sectionIndex":2,
            "sectionTitle":"section title",
            "sectionDescription":"section desc.",
            "questions":[
                {
                    "questionIndex":1,
                    "questionTitle":"quest. title ",
                    "questionType":"quest. type",
                    "value":"quest. answer", //<- This could be the field to update
                    "mandatory":true,
                    "multiline":false,
                    "adornment":"",
                    "restrictions":{"min":0,"max":10},
                    "options":[{"option":"option 1"},{"option":"option 2"},{"option":"option 3"}]
                },
                {
                    "questionIndex":2,
                    "questionTitle":"quest. title ",
                    "questionType":"quest. type",
                    "value":"quest. answer", //<- This could be the field to update
                    "mandatory":true,
                    "multiline":false,
                    "adornment":"",
                    "restrictions":{"min":0,"max":10},
                    "options":[{"option":"option 1"},{"option":"option 2"},{"option":"option 3"}]
                }
              ]
}

我要实现的目标:

使用value部分中的questionIndex:X访问问题的字段sectionIndex:Y,并更新所说的value

到目前为止,我已经尝试过:

到目前为止,我已在端点控制器上尝试过此操作:

{
    let query = {pollId: req.body.pollId, sectionIndex: req.body.sectionIndex, questionIndex: req.body.questionIndex}
    pollSchema.findOneAndUpdate(query,{$set :{value: req.body.value}},{new:true},function(err)
    {
       res.status(200).send({message:"updated"});  
       (err)=>
        { 
            res.status(500).send(err);
            console.log(err);
        }
    
    });
}

但这似乎不起作用...

其他信息:

我正在用邮递员测试该Enpoint,并将文档存储在MongoDB Atlas集群上。

如果您需要更多信息,请随时询问。预先感谢

1 个答案:

答案 0 :(得分:0)

好,所以我设法弄清楚了。

如果有人感兴趣,我会留下答案

pollSchema.findOneAndUpdate(pollId: req.body.pollId, "sections.questions.questionIndex": req.body.questionIndex,{$set: {"sections.$[s].questions.$.value": req.body.value}},{arrayFilters: [{"s.sectionIndex": req.body.sectionIndex}]})

有关更多信息,请访问:

$ operator

$[]

$[< identifier >]

我建议您完整地阅读这些文章,但是您仅通过那些超链接上的部分就可以理解这一点