【发布时间】:2018-07-12 19:20:56
【问题描述】:
我想更新子文档的值,然后保存主文档。因此,对子文档的更改将保存到数据库中。 这是我的代码ATM, 我找到了正确的子文档,这不是我猜的问题。但它不会保存更改。
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))
我的对象有点复杂,它必须是......它看起来像这样:
project={
prop,
prop,
prop,
qp:{
prop,
prop,
prop,
questions:[
question:{
prop,
prop,
prop,
answers:[
answerModels<--!this i want to find and edit!-->
]
}
]
}
}
【问题讨论】:
标签: javascript node.js mongodb mongoose mongoose-schema