MongoDB 中的 replaceOne() 和 updateOne() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35848688/
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
What's the difference between replaceOne() and updateOne() in MongoDB?
提问by Mike B.
MongoDB bulk operations have two options:
MongoDB 批量操作有两个选项:
Adds a single document update operation to a bulk operations list. The operation can either replace an existing document or update specific fields in an existing document.
Adds a single document replacement operation to a bulk operations list. Use the
Bulk.find()
method to specify the condition that determines which document to replace. TheBulk.find.replaceOne()
method limits the replacement to a single document.
将单个文档更新操作添加到批量操作列表。该操作可以替换现有文档或更新现有文档中的特定字段。
将单个文档替换操作添加到批量操作列表。使用该
Bulk.find()
方法指定确定要替换哪个文档的条件。该Bulk.find.replaceOne()
方法将替换限制为单个文档。
According to the documentation, both of these two methods can replace a matching document. Do I understand correctly, that updateOne()
is more general purpose method, which can either replace the document exactly like replaceOne()
does, or just update its specific fields?
根据文档,这两种方法都可以替换匹配的文档。我理解正确吗,这updateOne()
是一种更通用的方法,它可以完全像replaceOne()
那样替换文档,或者只是更新其特定字段?
回答by Hughzi
With replaceOne()
you can only replace the entire document, while updateOne()
allows for updating fields.
随着replaceOne()
你只能更换整个文档,同时updateOne()
允许更新的领域。
Since replaceOne()
replaces the entire document - fields in the old document not contained in the new will be lost. With updateOne()
new fields can be added without losing the fields in the old document.
由于replaceOne()
替换了整个文档 - 旧文档中未包含在新文档中的字段将丢失。随着updateOne()
可在不老的文档中丢失的领域增加新的领域。
For example if you have the following document:
例如,如果您有以下文档:
{
"_id" : ObjectId("0123456789abcdef01234567"),
"my_test_key3" : 3333
}
Using:
使用:
replaceOne({"_id" : ObjectId("0123456789abcdef01234567")}, { "my_test_key4" : 4})
results in:
结果是:
{
"_id" : ObjectId("0123456789abcdef01234567"),
"my_test_key4" : 4.0
}
Using:
使用:
updateOne({"_id" : ObjectId("0123456789abcdef01234567")}, {$set: { "my_test_key4" : 4}})
results in:
结果是:
{
"_id" : ObjectId("0123456789abcdef01234567"),
"my_test_key3" : 3333.0,
"my_test_key4" : 4.0
}
Note that with updateOne()
you can use the update operatorson documents.
请注意,updateOne()
您可以在文档上使用更新运算符。
回答by Jurgen Strydom
replaceOne()
replaces the entire document, while updateOne()
allows for updating or adding fields. When using updateOne()
you also have access to the update operatorswhich can reliably perform updates on documents. For example two clients can "simultaneously" increment a value on the same field in the same document and both increments will be captured, while with a replace the one may overwrite the other potentially losing one of the increments.
replaceOne()
替换整个文档,同时updateOne()
允许更新或添加字段。使用时,updateOne()
您还可以访问可以可靠地对文档执行更新的更新操作符。例如,两个客户端可以“同时”在同一文档中的同一字段上增加一个值,并且两个增量都将被捕获,而替换一个可能会覆盖另一个可能丢失其中一个增量。
Since replaceOne()
replaces the entire document - fields in the old document not contained in the new will be lost. With updateOne()
new fields can be added without losing the fields in the old document.
由于replaceOne()
替换了整个文档 - 旧文档中未包含在新文档中的字段将丢失。随着updateOne()
可在不老的文档中丢失的领域增加新的领域。
For example if you have the following document:
例如,如果您有以下文档:
{
"_id" : ObjectId("0123456789abcdef01234567"),
"my_test_key3" : 3333
}
Using:
使用:
replaceOne({"_id" : ObjectId("0123456789abcdef01234567")}, { "my_test_key4" : 4})
results in:
结果是:
{
"_id" : ObjectId("0123456789abcdef01234567"),
"my_test_key4" : 4.0
}
Using:
使用:
updateOne({"_id" : ObjectId("0123456789abcdef01234567")}, {$set: { "my_test_key4" : 4}})
results in:
结果是:
{
"_id" : ObjectId("0123456789abcdef01234567"),
"my_test_key3" : 3333.0,
"my_test_key4" : 4.0
}
回答by Evangelos
db.collection.replaceOne()
does exactly the same thing as db.collection.updateOne()
.
db.collection.replaceOne()
做完全一样的事情db.collection.updateOne()
。
The main difference is that db.collection.replaceOne()
's data that are being edited will have to go back and forth to the server, whereas db.collection.UpdateOne()
will request only the filtered ones and not the whole document!
主要区别在于db.collection.replaceOne()
正在编辑的 's 数据必须来回发送到服务器,而db.collection.UpdateOne()
只会请求过滤的数据而不是整个文档!