mongodb 错误:更新操作文档必须包含原子操作符,运行updateOne时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38883285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: the update operation document must contain atomic operators, when running updateOne
提问by Tim
In my collection, there is only one document.
在我的收藏中,只有一份文件。
> db.c20160712.find()
{ "_id" : ObjectId("57ab909791c3b3a393e9e277"), "Dimension_id" : 2, "Attribute" : "good", "Hour" : "20160712_06", "Frequency_count" : 100
I want to run updateOne
to replace the document with another one. But why is there Error: the update operation document must contain atomic operators
?
我想运行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
The second and third arguments in the above command comes from an example in The Definitive Guide to MongoDB: A complete guide to dealing with Big Data ... By Eelco Plugge, David Hows, Peter Membrey, Tim Hawkins
上述命令中的第二个和第三个参数来自The Definitive Guide to MongoDB: A complete guide to deal with Big Data ... 由 Eelco Plugge、David Hows、Peter Membrey、Tim Hawkins 提供
My MongoDB is 3.2.
我的 MongoDB 是 3.2。
回答by Alex Blex
回答by dyouberg
I believe this was changed as a side-effect of introducing the updateOne()
method in addition to update()
and updateMany()
as somewhat of a safeguard to prevent user's from accidentally overriding an entire document.
我相信这是作为引入该updateOne()
方法的副作用而改变的,update()
并且updateMany()
在某种程度上是为了防止用户意外覆盖整个文档。
You can use the replaceOne()
method instead, or an update()
without specifying multi:true
.
您可以改用该replaceOne()
方法,或者update()
不指定multi:true
.
回答by nagender pratap chauhan
You should use this code because I was also facing the same problem and then I used this code:
您应该使用此代码,因为我也遇到了同样的问题,然后我使用了此代码:
updateOne(
{ _id: new ObjectID(req.params.id) },
{ $set: { title: req.body.bookName, author: req.body.authorName } },
{ upsert: true }
)
and you should also define ObjectID
otherwise the problem will occur again....
并且您还应该定义ObjectID
否则问题将再次发生....
const ObjectID = require('mongodb').ObjectID;
const ObjectID = require('mongodb').ObjectID;
回答by Prasad Naik
You did the same mistake as I did. Upon going through the docs I realized the syntax is wrong. Try:
你和我犯了同样的错误。在浏览文档时,我意识到语法是错误的。尝试:
db.c20160712.updateOne(
{ "Attribute" : "good"},
{"Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Genre" : "Action"},
{ upsert: true}
)