Mongo / Mongooseprimefaces更新值无效错误

我正在尝试编写使用Mongoose findOneAndUpdate函数来写入对Mongo文档的更新。 从本质上讲,我有一个文件,其中有另一个模式的数组,当我试图追加更多的这些模式types,我得到以下错误:

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

唯一的区别是,当我检索它时,有三个更less的模块,我追加了一些新的数组。

想听听任何想法,至less是错误意味着什么!

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

 object = object.toString() 

并删除任何潜在的ID属性

 delete object._id 

或干脆

 object = object.toObject(); 

我有同样的问题,原来我正在使用$推不正确。 我在做

 {$push:thing_to_push} 

但它需要

 {$push:{list_to_push_into:thing_to_push}} 

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

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

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

如果找不到,则创build一个新的,更新现有的。

我知道有两个操作。 但我一直没有find办法一步到位。

另一件要检查的是,如果你正在发送传递一组变化$ set。 调用如下所示的错误消息:

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

给我的[错误:$ set的无效primefaces更新值。 预计一个对象,收到对象]

将其更改为一个对象工作。

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

尝试传递更新参数值作为string,而不是mongoose模型对象。 当我使用传递模型对象时,我得到同样的错误。 以下是代码差异。

有问题的代码:

 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) { ... }