在猫鼬不工作的情况下更新子数组对象 [英] Updating subarray object with Mongoose not working

查看:71
本文介绍了在猫鼬不工作的情况下更新子数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MongoDB数据库主文档如下:

My MongoDB database main document looks like this:

    {
        "_id": {
            "$oid": "568ad3db59b494d4284ac191"
        },
        "name": "Myclass",
        "items": [
          {
            "name": "Conductive Gel",
            "internal_id": "ID00067",
            "description": "ECG Conductive Gel",
            "short_description": "ECG Conduct. Gel",
            "providers": [
              {
                "name": "one",
                "address": ""
              },
              {
                "name": "two",
                "address": ""
              }
            ]

          },
          {

          }
        ]
   }

好的,那是我收到一个应该更新其中一项(与_id匹配的项)的ajax put调用.

Ok the thing is that I am receiving an ajax put call that should update one of the items (the one that matches the _id).

我的方法:

exports.updateItem = function(req, res, next) { 


              classes.findOne({_id: '568ad3db59b494d4284ac19d'}, function(e,myclass){
                    if(!e) {

                      myclass.items.forEach(function(item){
                        if (item._id == req.body._id) {

                            item = req.body;
                            myclass.save(function(err, doc){


                                if (err) return next(err);
                                return res.status(200).send('The item has been updated!');
                            });
                        }                              
                      });

                    } else {
                        res.status(500).send('Class not folund in BBDD!!');
                    }
                });
};

问题是,当我执行item = req.body;时,req.body信息未映射到项目猫鼬对象中,并且数据库中的项目也未更新.我也没有任何错误.

The thing is that when I do item = req.body; the req.body info is not mapped into the item mongoose object and the item in the database is not updated. I don't get any error either.

在执行item = req.body;的那一刻,我检查了req.body和item是否具有完全相同的字段.

I have checked that req.body and item both have the exact same fields in the moment that I do the item = req.body; .

另一方面,如果我执行item.name='whatever',它会起作用.

If I do item.name='whatever' , on the other hand, it works.

我已经为这个问题奋战了4个小时,没有解决办法...

I've been fighting with this issue for 4 hours now without a solution...

我还尝试了Mongoose的findOneAndUpdate()查询,但没有成功.

I have also tried Mongoose's findOneAndUpdate() query without success..

推荐答案

如果将item分配给新值,则实际上并没有更改数组的内容. item只是对数组中元素的引用.

If you assign item to a new value you are not actually changing the content of the array. item is just a reference to the element in the array.

您可能想要的是通过合并两个对象itemreq.body来编辑数组的内容.

What you probably want is to edit the content of the array by merging the two objects item and req.body.

require('extend'); // npm install extend
extend(item, req.body);

这实际上将更新数组中的值,然后将其保存.

That will actually update the value in the array which will then be saved.

但是,我建议使用猫鼬来更新子文档,如下所述:猫鼬查找/更新子文档

However, I recommend updating the subdocument using mongoose as explained here: Mongoose find/update subdocument

这篇关于在猫鼬不工作的情况下更新子数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆