I want to update a subdocument's value and after that save the main document. So the changes to the subdocument gets saved to the database. Here is my code ATM, I find the correct subdocument, that is not the problem I guess. But it doesn't save the changes.
projectModel.findById({_id: req.body.projectId})
.then(function(doc){
question = doc.qp.questions.filter(q => {return q._id == req.body.questionId})[0];
var ans = question.answers.filter(a => {return a._id == req.body.answerId})[0];
if(ans)
{console.log(ans)}
ans.value = req.body.answerValue;
doc.save().then(res.send({questionId: req.body.questionId,answerId: req.body.answerId,answerValue: req.body.answerValue}))
})
.catch(error => console.log(error))
my object is kinda complex, and it has to be.. it looks something like this:
project={
prop,
prop,
prop,
qp:{
prop,
prop,
prop,
questions:[
question:{
prop,
prop,
prop,
answers:[
answerModels<--!this i want to find and edit!-->
]
}
]
}
}