【问题标题】:Mongo/Mongoose Invalid atomic update value errorMongo/Mongoose Invalid atomic update value 错误
【发布时间】:2013-07-13 21:01:43
【问题描述】:

我正在尝试使用 Mongoose findOneAndUpdate 函数编写对 Mongo 文档的更新。本质上,我有一个文档,其中包含另一个架构的数组,当我尝试附加更多这些架构类型时,我收到以下错误:

[Error: Invalid atomic update value for $__. Expected an object, received object]

我很难弄清楚这个错误的含义,更不用说它的来源了。

我正在尝试更新的数据如下:

{ section_id: 51e427ac550dabbb0900000d,
version_id: 7,
last_editor_id: 51ca0c4b5b0669307000000e,
changelog: 'Added modules via merge function.',
committed: true,
_id: 51e45c559b85903d0f00000a,
__v: 0,
modules: 
[ { orderId: 0,
type: 'test',
tags: [],
data: [],
images: [],
content: ["Some Content Here"] },
{ orderId: 1,
type: 'test',
tags: [],
data: [],
images: [],
content: ["Some Content Here"] },
{ orderId: 2,
type: 'test',
tags: [],
data: [],
images: [],
content: ["Some Content Here"] },
{ orderId: 3,
type: 'test',
tags: [],
data: [],
images: [],
content: ["Some Content Here"] },
{ orderId: 4,
type: 'test',
tags: [],
data: [],
images: [],
content: ["Some Content Here"] },
{ orderId: 5,
type: 'test',
tags: [],
data: [],
images: [],
content: ["Some Content Here"] } ] }

唯一的区别是,当我检索它时,会少三个模块,并且我将一些新的模块添加到数组中。

很想听听任何想法,至少关于错误的含义!

【问题讨论】:

    标签: node.js mongodb mongoose database


    【解决方案1】:

    这可能是因为更新后的对象仍然是 Mongoose 对象。 尝试在findOneAndUpdate之前将其转换为JS对象

    object = object.toString()
    

    并删除任何潜在的 ID 属性

    delete object._id
    

    或者干脆

    object = object.toObject();
    

    【讨论】:

    • 可以使用 .toObject() 将 mongooseObject 转换为对象
    • 当你使用 .toObject() 时,它会自动删除 _id 字段吗?
    • 感谢 2020 年的您。现在世界上的情况很糟糕,但这条评论帮助了我。
    【解决方案2】:

    尝试将更新参数值作为字符串而不是猫鼬模型对象传递。当我用来传递模型对象时,我遇到了同样的错误。下面是代码区别。

    有问题的代码:

    updateUser: function(req, res) {
    **var updatedUserModel = new Users(req.body);**
        Users.findOneAndUpdate({tpx_id:req.params.id}, {$set:**updatedUserModel**}, function(err, User){
               ...
       }
    }
    

    工作代码:

    updateUser: function(req, res) {
       Users.findOneAndUpdate({tpx_id:req.params.id}, {$set:**req.body**}, function(err, User) {
       ...
    }
    

    【讨论】:

      【解决方案3】:

      要检查的另一件事是,您是否将一组更改传递给 $set。我在调用这样的东西时收到的错误消息:

      db.products.update( { sku: "abc123" },
                      { $set: [
                               {quantity: 500},
                               {instock: true}
                              ]
                      }
                    )
      

      给了我[错误:$set 的原子更新值无效。期望对象,接收对象]

      将其更改为对象有效。

      db.products.update( { sku: "abc123" },
                      { $set: {
                                quantity: 500,
                                instock: true
                              }
                      }
                    )
      

      【讨论】:

        【解决方案4】:

        @Magrelo 和@plus 引导我找到了一个有效的答案。比如:

        MyModel.findOneAndUpdate({ section_id: '51e427ac550dabbb0900000d' }, mongooseObject.toObject(), { upsert: true }, function(err, results) {
            //...
        });
        

        【讨论】:

        • mongooseObject.toObject() 这是有帮助的,谢谢
        【解决方案5】:

        我遇到了同样的问题,结果发现我错误地使用了 $push。我在做

        {$push:thing_to_push}
        

        但它必须是

        {$push:{list_to_push_into:thing_to_push}}
        

        【讨论】:

          【解决方案6】:

          我有同样的问题。我最终使用了finOne() method

          如果没有找到则创建一个新的,如果找到则更新现有的。

          我知道有两个操作。但我只是没有找到任何方法可以一步完成。

          【讨论】:

            猜你喜欢
            • 2023-02-20
            • 2021-04-14
            • 2016-05-21
            • 2021-12-20
            • 2017-03-23
            • 1970-01-01
            • 2019-11-21
            • 2020-10-24
            • 2013-08-27
            相关资源
            最近更新 更多