MongoDB-Mongoose:如何使用Mongoose同时更新两个单独的嵌入式文档?

AllJs

所以我想更新域status我的Report文档,我的Station.reports子文档是对象的数组,在一个单一的API调用。问题是在进行API调用时,我能够更新报告文档,但不能更新站点文档。调用之后,将console.log(station.reports);返回预期的子文档:,[{"_id":"588fed278b50cd180bd6cc15","date":"2017-01-31T01:48:57.487Z","status":"Archived"}]但是该子文档未保存在我的DB中相应的Station文档中。请在这里我需要帮助。谢谢。

车站文件:

{
    "_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);
        })
    });
})
AllJs

经过多次尝试,并在Ravi的帮助下,我找到了一个对我来说非常有效的解决方案。唯一改变的是我的API电话。其余代码未更改。
希望这可以帮助有类似需求的人。

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, info) {
            if(err) return console.log(err);

            info.reports.forEach(function(rpt){
                if (rpt._id == req.params.id){

                    Station.update({_id:info._id, "reports._id":rpt._id },
                        {
                            $set:{"reports.$.status": req.body.status }
                        },function (err, results) {
                            if(err) return console.log("This Station couldn't be updated " + err);
                            console.log(results)
                        }
                    )
                }
            })
            report.save(function (err, report) {
                if (err)
                    return res.send(err); 

                res.json({report:report, station:info});
            });
        })
    });
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在MongoDB中使用数组获取嵌入式文档(使用Mongoose)

如何使用Mongoose将值推入MongoDB中嵌入式文档中的数组?

使用Mongoose从Node从MongoDB中的复杂嵌入式集合中插入,更新和删除

嵌入文档-Mongoose与MongoDB

如何使用 Mongoose 更新 MongoDB 中的文档

何时使用嵌入式文档MongoDB

MongoDB-更新嵌入式文档和文档本身

更新mongodb中arrays对象的arrays的嵌入式文档

更新Mongodb中的嵌入式文档:性能问题?

在mongodb中更新嵌套的嵌入式文档

如何使用Mongodb / Mongoose中的子文档处理深度更新?

如何使用Node.js / Mongoose / MongoDB同时更新2个集合

如何使用MongoDB或Mongoose在一个查询中对两个集合进行分组

如何在 MongoDb 中查询嵌入式文档?

如何在mongodb中搜索嵌入式文档?

使用mgo对mongoDB中的嵌入式文档进行部分更新

使用C#获取和添加/更新多层嵌入式/嵌套MongoDB文档

在mongodb nodejs中更新双嵌套的嵌入式文档,而无需使用数字索引

使用Presto查询MongoDB嵌入式/嵌套文档的数组

使用Spring Boot的MongoDB嵌入式文档

使用python(pymongo)在mongodb中编辑嵌入式文档

使用JSON访问mongodb的嵌入式文档

使用 Java 管理 MongoDB 嵌入式文档

查询MongoDB中的嵌入式文档

在mongodb中展开嵌入式文档

mongodb汇总嵌入式文档值

Mongodb对嵌入式文档的本机验证

mongodb嵌入式文档搜索

Mongodb嵌入式文档最佳实践