11

I am trying to write to write an update to a Mongo document using the Mongoose findOneAndUpdate function. Essentially, I have a document that has an array of another Schema in it, and when I attempt to append more of those schema type, I get the following error:

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

I'm having a hard time figuring out what this error even means, much less what its source is.

The data I'm attempting to update is as follows:

{ 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"] } ] }

The only difference is that when I retrieve it, there are three fewer modules, and I append some new ones to the array.

Would love to hear any thoughts, at least as to what the error means!

4

6 回答 6

15

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

object = object.toString()

并删除任何潜在的 ID 属性

delete object._id

或者干脆

object = object.toObject();
于 2013-08-02T16:08:24.523 回答
3

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

{$push:thing_to_push}

但它必须是

{$push:{list_to_push_into:thing_to_push}}
于 2013-10-10T18:53:29.503 回答
3

@Magrelo 和 @plus 让我找到了一个有效的答案。就像是:

MyModel.findOneAndUpdate({ section_id: '51e427ac550dabbb0900000d' }, mongooseObject.toObject(), { upsert: true }, function(err, results) {
    //...
});
于 2014-01-24T23:23:10.490 回答
1

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

有问题的代码:

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) {
   ...
}
于 2015-01-06T17:10:23.977 回答
0

我遇到过同样的问题。我最终使用了finOne() 方法

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

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

于 2013-07-17T04:11:19.170 回答
0

另一件要检查的事情是,您是否正在向 $set 发送一组更改。我在调用这样的东西时收到的错误消息:

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

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

将其更改为对象有效。

db.products.update( { sku: "abc123" },
                { $set: {
                          quantity: 500,
                          instock: true
                        }
                }
              )
于 2014-05-27T12:56:27.390 回答