如何使用 Mongoose 更新给定另一个嵌套字段值的嵌套字段?
How can I update a nested field given another nested field value with Mongoose?
我正在创建一个端点,该端点旨在更新之前已经回答过的问题的答案,但我无法访问我的 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"}]
}
]
}
我想达到的效果:
在 sectionIndex:Y
部分的 questionIndex:X
问题上访问字段 value
并更新 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);
}
});
}
但这似乎不起作用...
附加信息:
我正在使用 Postman 测试 enpoint 并将文档存储在 MongoDB Atlas 集群上。
如果您需要更多信息,请随时询问。提前致谢
好的,所以我设法弄明白了。
如果有人感兴趣我会留下答案
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}]})
有关更多信息,请访问:
我建议您完整阅读这些文章,但您只需阅读这些超链接上的部分就会明白其中的意思
我正在创建一个端点,该端点旨在更新之前已经回答过的问题的答案,但我无法访问我的 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"}]
}
]
}
我想达到的效果:
在 sectionIndex:Y
部分的 questionIndex:X
问题上访问字段 value
并更新 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);
}
});
}
但这似乎不起作用...
附加信息:
我正在使用 Postman 测试 enpoint 并将文档存储在 MongoDB Atlas 集群上。
如果您需要更多信息,请随时询问。提前致谢
好的,所以我设法弄明白了。
如果有人感兴趣我会留下答案
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}]})
有关更多信息,请访问:
我建议您完整阅读这些文章,但您只需阅读这些超链接上的部分就会明白其中的意思