MongoDB findOneAndDelete findOneAndUpdate findOneAndReplace
Introduction
In MongoDB, the findOne method is used to get the first matched document from a collection. It is useful to perform operations on a single document. MongoDB provides an easier way for it. There are three methods that are methods combined with the findOne method that can be used effectively to work with databases. These are findOneAndDelete, findOneAndReplace and findOneAndUpdate. In this article, we will discuss these three methods.
We will use these methods using the details
collection.
1 2 3 4 5 | { "_id" : ObjectId("5d715243c452f78e335d2d8c"), "name" : "Sam", "age" : 23, "location" : "Chicago" } { "_id" : ObjectId("5d71524ec452f78e335d2d8d"), "name" : "Lisa", "age" : 25, "location" : "Texas" } { "_id" : ObjectId("5d735cf0022a29e7dba014e3"), "name" : "John", "age" : 21, "location" : "New York" } { "_id" : ObjectId("5d735d0e022a29e7dba014e4"), "name" : "Max", "age" : 23, "location" : "New York" } { "_id" : ObjectId("5d735d1f022a29e7dba014e5"), "name" : "Ronn", "age" : 29, "location" : "Detroit" } |
findOneAndDelete
The findOneAndDelete method finds a document in a collection and deletes it. There can be two scenarios for this method.
If a filter condition is provided, it will delete the first matched document.
If a filter condition is not provided, it will delete the very first document of the collection.
Let’s try this.
1 2 3 4 5 6 7 8 | > db.details.findOneAndDelete({"name":"John"}) { "_id" : ObjectId("5d735cf0022a29e7dba014e3"), "name" : "John", "age" : 21, "location" : "New York" } > |
We deleted the document where the name is John. But observe the output. The findOneAndDelete method returns the original document before deleting. Let’s verify if the document was deleted or not.
1 2 3 4 | { "_id" : ObjectId("5d715243c452f78e335d2d8c"), "name" : "Sam", "age" : 23, "location" : "Chicago" } { "_id" : ObjectId("5d71524ec452f78e335d2d8d"), "name" : "Lisa", "age" : 25, "location" : "Texas" } { "_id" : ObjectId("5d735d0e022a29e7dba014e4"), "name" : "Max", "age" : 23, "location" : "New York" } { "_id" : ObjectId("5d735d1f022a29e7dba014e5"), "name" : "Ronn", "age" : 29, "location" : "Detroit" } |
Yes, there is no document with the name John in the details
collection.
findOneAndReplace
The findOneAndReplace method finds a document and replaces it with another. There are two arguments of this method – an object containing filter condition and an object containing the new document. This method will find the document according to the condition and completely replaces it with a new one. If a condition is not provided, it will replace the very first document of the collection. But remember, no condition means an empty object. If the second argument is provided as the first, it will throw an error.
Let’s replace the document where the name is Lisa.
1 2 3 4 5 6 7 8 | > db.details.findOneAndReplace({"name":"Lisa"}, {"name": "Sunny", "age" :27, "location" : "New York"}) { "_id" : ObjectId("5d71524ec452f78e335d2d8d"), "name" : "Lisa", "age" : 25, "location" : "Texas" } > |
It also returns the original document that was replaced. Let’s verify if it worked correctly or not.
1 2 3 4 5 6 | > db.details.find() { "_id" : ObjectId("5d715243c452f78e335d2d8c"), "name" : "Sam", "age" : 23, "location" : "Chicago" } { "_id" : ObjectId("5d71524ec452f78e335d2d8d"), "name" : "Sunny", "age" : 27, "location" : "New York" } { "_id" : ObjectId("5d735d0e022a29e7dba014e4"), "name" : "Max", "age" : 23, "location" : "New York" } { "_id" : ObjectId("5d735d1f022a29e7dba014e5"), "name" : "Ronn", "age" : 29, "location" : "Detroit" } > |
Yes, the document was replaced by the new one.
Let’s see what happens when only a single argument is provided.
1 2 3 4 5 | > db.details.findOneAndReplace({"name": "Sunny", "age" :27, "location" : "New York"}) 2019-09-07T13:15:21.251+0530 E QUERY [js] TypeError: can't convert undefined to object : DBCollection.prototype.findOneAndReplace@src/mongo/shell/crud_api.js:751:16 @(shell):1:1 > |
Here, we did not specify the condition, even we did not pass an empty object. This is why it returned an error.
findOneAndUpdate
The findOneAndUpdate method finds a document and updates it. It also has two arguments – an object containing condition and an object containing the update. Similar to findOneAndReplace, if the condition is not provided(no condition means an empty object), it will update the very first document of the collection.
Let’s try to update the document where the name is Max. We will change the location from New York to California.
1 2 3 4 5 6 7 8 | > db.details.findOneAndUpdate({"name": "Max"}, { $set: {"location": "California"}}) { "_id" : ObjectId("5d735d0e022a29e7dba014e4"), "name" : "Max", "age" : 23, "location" : "New York" } > |
As we are performing an update operation, we need an atomic operator. Here, we used $set. It returns the original document.
Let’s verify if the update was successful or not.
1 2 3 4 5 6 | > db.details.find() { "_id" : ObjectId("5d715243c452f78e335d2d8c"), "name" : "Sam", "age" : 23, "location" : "Chicago" } { "_id" : ObjectId("5d71524ec452f78e335d2d8d"), "name" : "Sunny", "age" : 27, "location" : "New York" } { "_id" : ObjectId("5d735d0e022a29e7dba014e4"), "name" : "Max", "age" : 23, "location" : "California" } { "_id" : ObjectId("5d735d1f022a29e7dba014e5"), "name" : "Ronn", "age" : 29, "location" : "Detroit" } > |
Yes, the location of the document where the name is Max is updated to California.
Conclusion
The fineOneAndDelete, findOneAndUpdate, and findOneAnd Replace methods are very useful when working with MongoDB so we hope this quick tutorial was informative for you and can apply it to your problem at hand. Below we have a section for all the code that was demonstrated.
Just the Code
Here’s the code for the usage of each of the three methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | > db.details.findOneAndDelete({"name":"John"}) { "_id" : ObjectId("5d735cf0022a29e7dba014e3"), "name" : "John", "age" : 21, "location" : "New York" } > ``` ```js > db.details.findOneAndReplace({"name":"Lisa"}, {"name": "Sunny", "age" :27, "location" : "New York"}) { "_id" : ObjectId("5d71524ec452f78e335d2d8d"), "name" : "Lisa", "age" : 25, "location" : "Texas" } > ``` ```js > db.details.findOneAndUpdate({"name": "Max"}, { $set: {"location": "California"}}) { "_id" : ObjectId("5d735d0e022a29e7dba014e4"), "name" : "Max", "age" : 23, "location" : "New York" } > |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started