在我的收藏中,只有一个文件.
> db.c20160712.find()
{ "_id" : ObjectId("57ab909791c3b3a393e9e277"), "Dimension_id" : 2, "Attribute" : "good", "Hour" : "20160712_06", "Frequency_count" : 100
Run Code Online (Sandbox Code Playgroud)
我想运行updateOne以替换另一个文档.但为什么会这样Error: the update operation document must contain atomic operators?
> db.c20160712.updateOne( { "Attribute" : "good"}, {"Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Genre" : "Action"}, { upsert: true} )
2016-08-10T16:37:57.089-0400 E QUERY [thread1] Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateOne@src/mongo/shell/crud_api.js:493:1
@(shell):1:1
Run Code Online (Sandbox Code Playgroud)
上述命令中的第二个和第三个参数来自MongoDB的权威指南中的一个示例:处理大数据的完整指南...作者:Eelco Plugge,David Hows,Peter Membrey,Tim Hawkins
我的MongoDB是3.2.
Ale*_*lex 74
第二个参数的语法错误.请查看文档.它应该是:
db.c20160712.updateOne(
{ "Attribute" : "good" },
{ $set: {"Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Genre" : "Action" } },
{ upsert: true }
);
Run Code Online (Sandbox Code Playgroud)
dyo*_*erg 17
我认为除了update()和updateMany()之外,引入updateOne方法的一个副作用已经改变了,这有点可以防止用户意外覆盖整个文档.
您可以使用replaceOne()方法,或使用update()而不指定multi:true.
您应该使用此代码,因为我也遇到了相同的问题,然后使用了以下代码:
updateOne(
{ _id: new ObjectID(req.params.id) },
{ $set: { title: req.body.bookName, author: req.body.authorName } },
{ upsert: true }
)
Run Code Online (Sandbox Code Playgroud)
并且您还应该定义ObjectID问题,否则问题将再次发生。
const ObjectID = require('mongodb').ObjectID;
| 归档时间: |
|
| 查看次数: |
43686 次 |
| 最近记录: |