I'm creating an endpoint which is intended to update an answer of a question already answered before, but I'm having troubles accessing that field on my Mongoose schema.
The schema I'm working with responds to this JSON example:
{
"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"}]
}
]
}
What I want to achieve:
Access the field value
on the question with questionIndex:X
on the section with sectionIndex:Y
and update said value
.
What I've tried so far:
So far I tried this on my endpoint controller:
{
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);
}
});
}
But this doesn't seems to work...
Additional Information:
I'm testing the enpoint with Postman and storing the documents on a MongoDB Atlas Cluster.
If you need more information feel free to ask. Thanks in advance
Ok, so I managed to figure it out.
I'll leave the answer if someone is interested
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}]})
For more information go to:
I'd recommend to read these articles completely, but you'll get the idea just with the sections on those hyperlinks