Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between replaceOne() and updateOne() in MongoDB?

MongoDB bulk operations have two options:

  1. Bulk.find.updateOne()

    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.

  2. Bulk.find.replaceOne()

    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. The Bulk.find.replaceOne() method limits the replacement to a single document.

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?

like image 658
Mike Avatar asked Mar 07 '16 16:03

Mike


People also ask

What is the difference between update and updateOne in MongoDB?

updateMany() : It update all documents in a collection with matching filter. updateOne() : It update only one top most document in a collection with matching filter. update() : By default, the update() method updates a single document.

What does updateOne do in MongoDB?

In MongoDB, updateOne() method updates a first matched document within the collection based on the given query. When you update your document the value of the _id field remains unchanged. This method updates one document at a time and can also add new fields in the given document.

What is Upsert MongoDB?

In MongoDB, upsert is an option that is used for update operation e.g. update(), findAndModify(), etc. Or in other words, upsert is a combination of update and insert (update + insert = upsert).


2 Answers

With replaceOne() you can only replace the entire document, while updateOne() allows for updating fields.

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.

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 operators on documents.

like image 78
Hughzi Avatar answered Oct 10 '22 05:10

Hughzi


replaceOne() replaces the entire document, while updateOne() allows for updating or adding fields. When using updateOne() you also have access to the update operators which 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.

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.

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
}
like image 45
Jurgen Strydom Avatar answered Oct 10 '22 06:10

Jurgen Strydom