Updated March 6, 2023
Introduction to Mongodb findOneAndUpdate
MongoDB findOneAndUpdate method is used to update single documents from the collection; we can update the single documents based on sort criteria and filter which was we have used in the query. We need to use the collection name with the findOneAndUpdate method to update the document from the specified collection. We can use multiple parameters like upsert, returnNewDocument, and arrayFilters in the findOneAndUpdate method; also, the findOneAndUpdate method will update the first document which matches the selection criteria of the query. If our selection criteria matches more than one documents in the collection findOneAndUpdate method will update only the first matched document.
Syntax
Below is the syntax of the MongoDB findOneAndUpdate method. We can update a single document by using this method.
db.name_of_collection.findOneAndUpdate (
{Selection criteria of update query using findOneAndUpdate method}, {$set operator} {Upsert (optional), returnNewDocument (Optional), collation (optional), arrayFilters (Optional), sort (optional)}
)
Parameter description of findOneAndUpdate method in MongoDB.
- Name of the collection – This parameter is defined as collection name, which was we have used with the findOneAndUpdate method to update the single documents.
- findOneAndUpdate – This is the method name used in MongoDB to update single documents within the collection.
- Selection criteria of update query – In this section, we have defined the selection criteria of updated documents. Which documents we have updating it’s defined in this selection criteria.
- $set operator – This operator is used in MongoDB while updating any documents. It will update or replace the existing value with a new value.
- Upsert – It is an optional parameter used with the findOneAndUpdate method in MongoDB. To use this parameter with the findOneAndUpdate method, we need to define this parameter value as true in our query. The default value of this parameter is false.
- Collation – This is also the default parameter used with the findOneAndUpdate method in MongoDB. This parameter will specify the use of collation operations in MongoDB. While using this parameter with the findOneAndUpdate method, it will allow the language-specific rules to the user.
- ArrayFilters – The type of this parameter in the findOneAndUpdate method is an array. This parameter shows which array we have modifying using findOneAndUpdate
- Sort – This parameter will determine as which document we have modifying. Type of sort parameter argument is a document.
- returnNewDocument – The type of this parameter in the findOneAndUpdate method is boolean. It’s an optional parameter while using the findOneAndUpdate method in MongoDB. By default, this parameter will return the original document; to return the updated document, we need to use the returnNewDocument parameter with the findOneAndUpdate method.
How findOneAndUpdate works in Mongodb?
We need to pass a three-parameter with the findOneAndUpdate method in MongoDB. The first two parameters are mandatory, while the third parameter is optional.
1) Selection criteria of update query
2) New data to be updated
3) Optional parameter
- When we are updating any document using the findOneAndUpdate method, the method will return the original document. It will not return the updated documents as a result.
- In the below example, we have seen that we have updated the name of id 10 from ABC to MNP. After executing the query, it will show all the original details of documents which were id as 10. It will not show the updated details, but in collection details, we can see that the name is updated from ABC to MNP for id 10 documents.
Code:
db.MongoDB_Update.find ()
db.MongoDB_Update.findOneAndUpdate ({name: "ABC"}, {$set: {name: "MNP"}})
db.MongoDB_Update.find ()
Figure – The findOneAndUpdate method will return the original document.
- If we want to return the updated document, then we need to pass the returnNewDocument parameter value as true with the findOneAndUpdate method.
- If we have not found any matching documents while searching using selection criteria, then the findOneAndUpdate method will return the null value.
- The below example shows that the findOneAndUpdate method will return the null value if we have not found any matching document in the collection.
- In the below example, we have to find the document name as ABC, but the document name as ABC is not present in collection MongoDB_Update collection, so it will return the null values.
Code:
db.MongoDB_Update.findOneAndUpdate ({name: "ABC"}, {$set: {name: "MNP"}})
db.MongoDB_Update.find ()
Figure – Example findOneAndUpdate method returns the null value if not found any matching document from the collection.
Example
- The below example shows the findOneAndUpdate method in MongoDB.
Update single document using findOneAndUpdate method –
- In the below example, we have updated the name as MNP to ABC. We can see that the first name contains MNP is updated with the name as ABC.
Code:
db.MongoDB_Update.find ()
db.MongoDB_Update.findOneAndUpdate ({name: "MNP"}, {$set: {name: "ABC"}})
db.MongoDB_Update.find ()
Figure – Example to update single document using findOneAndUpdate method.
- Update embedded document using findOneAndUpdate method –
- In the below example, we have updated the embedded documents. We have updated the lap_spec set for the name of the ABC field.
Code:
db.MongoDB_Update.find ()
db.MongoDB_Update.findOneAndUpdate ({name: "ABC"}, {$set: {"lap_spec.ram": 128, "lap_spec.screen": 16.5, "lap_spec.cpu": 2.66}})
db.MongoDB_Update.find ()
Figure – Example to Update embedded document using findOneAndUpdate method.
- Update array elements using findOneAndUpdate method –
- In the below example, we have updated the array element documents. We have updated the lap_storage array for the name of ABC.
Code:
db.MongoDB_Update.find ()
db.MongoDB_Update.findOneAndUpdate ({name: "ABC"}, {$set: {"lap_storage.0": 8, "lap_storage.1": 16, "lap_storage.2": 32, "lap_storage.3": 64, "lap_storage.4": 128, "lap_storage.5": 256}})
db.MongoDB_Update.find ()
Figure – Example to update array elements using findOneAndUpdate method.
- Return the updated documents using the findOneAndUpdate method –
- In the below example, we have returned the updated documents. We can see that we have updated the lap_spec set, and this updated result is returned in the output.
- We have pass the true value of the returnNewDocument parameter with the findOneAndUpdate method.
Code:
db.MongoDB_Update.find ()
db.MongoDB_Update.findOneAndUpdate ({name: "XYZ"}, {$set: {"lap_spec.ram": 8, "lap_spec.screen": 16, "lap_spec.cpu": 1.66}}, {returnNewDocument: true})
db.MongoDB_Update.find ()
Figure – Example to return the updated documents using findOneAndUpdate method.
- Upsert parameter using findOneAndUpdate method –
- In the below example, we have used the upsert parameter. We have updated the name from ABC to CBZ.
Code:
db.MongoDB_Update.find ()
db.MongoDB_Update.findOneAndUpdate ({name: "ABC"}, {$set: {name: "CBZ"}}, {upsert: true, returnNewDocument: true})
db.MongoDB_Update.find ()
Figure – Example of Upsert parameter using findOneAndUpdate method.
Conclusion
We can update single documents using the findOneAndUpdate method. If more than one document matched in our selection criteria, then it will update the first document from the collection. This method will return the original document; if we want to return an updated value, then we need to use the returnNewDocument parameter.
Recommended Articles
This is a guide to Mongodb findOneAndUpdate. Here we discuss How findOneAndUpdate works in Mongodb along with the Parameter description of the findOneAndUpdate method in MongoDB. You may also have a look at the following articles to learn more –