所以我试图在我的Report
文档和我的Station.reports
子文档中更新字段status
,这是一个对象数组,在一个API调用中。问题是我能够在进行API调用时更新报告文档,但不能更新工作站文档。通话结束后,console.log(station.reports);
返回预期的子文档,即:[{"_id":"588fed278b50cd180bd6cc15","date":"2017-01-31T01:48:57.487Z","status":"Archived"}]
但是,这并未保存在我的数据库中相应的Station文档中。请在这里获得帮助。谢谢。如何使用Mongoose同时更新两个独立的嵌入式文档?
站文档:
{
"_id": "588a777d4e26720e7afa7e1e",
"phone": "(007) – 007 – 7007",
"name": "name1",
"email": "[email protected]",
"reports": [
{
"status": "Submitted",
"date": "2014-01-31T01:48:57.487Z",
"_id": "588fed278b50cd180bd6cc15"
}
]
}
报告文件
{
"_id": "588fed278b50cd180bd6cc15",
"description": "Description of the report",
"time": "05:48 PM",
"date": "2017-01-31T01:48:57.487Z",
"status": "Archived",
"location" : "123 Main Street"
"station" : "588a777d4e26720e7afa7e1e"
}
API调用
router.put('/reports/:id/updateStatus', function (req, res) {
Report.findById(req.params.id, function(err,report){
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
return res.send(err);
// Update the Report object
report.status = req.body.status;
// Update the Corresponding station.reports subdocument
Station.findOne({_id:report.station}, function (err, data) {
if(err) return console.log(err);
data.reports.forEach(function(rpt){
if (rpt._id == req.params.id){
rpt.status = req.body.status
data.save(function (err, station) {
if (err)
return res.send(err);
console.log(station.reports);
})
}
})
})
report.save(function (err, report) {
if (err)
return res.send(err);
res.json(report);
})
});
})
@Ravi嗨。感谢您的回复。我想尝试一下你的建议,但首先考虑到子文档'reports'是一个数组,你怎么可能写''reports._id“'。这可能会破坏整个事情吗?也许我错过了一些东西 – AllJs
不,不能,你可以像这样在数组中查询,'reports。$。status'将只更新报告数组的匹配元素 –
真的。我并不知道这种技术。好。现在让我试试看。 – AllJs