MongoDB – db.collection.findOneAndUpdate() Method
Last Updated :
04 Mar, 2025
The MongoDB findOneAndUpdate()
method is used to update the first matched document in a collection based on the selection criteria. It offers various options such as sorting, upserting, and returning the updated document. This method is a part of MongoDB’s CRUD operations and provides an easy-to-use interface for handling document updates efficiently.
What is FindOneAndUpdate() in MongoDB?
The MongoDB findOneAndUpdate() method updates the first matched document in the collection that matches the selection criteria. If more than one document matches the selection criteria then it updates only the first matched document. When we update the document, the value of the _id field remains unchanged.
This method will return the original document but if we want to return the updated document then we have to set the value of the returnNewDocument parameter to true. It takes three parameters, the first one is the selection criteria, the second one is the new data to be updated and the remaining are optional. Using this method we can also replace embedded documents. We can also use this method in multi-document transactions.
Syntax:
db.collection.findOneAndUpdate(
selection_criteria: <document>,
update_data: <document>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>,
collation: <document>,
arrayFilters: [ <filterdocument1>, … ]
})
Parameters:
- selection_criteria: (Required) A document containing the conditions used to find the document to be updated.
- update_data: (Required) A document that specifies the modifications to be made to the selected document.
- Optional Parameters:
- projection: Specifies which fields should be included or excluded in the returned document.
- sort: Determines the document to update when multiple documents match the selection criteria.
- maxTimeMS: Sets the maximum time (in milliseconds) for the operation to complete.
- upsert: If true, creates a new document if no document matches the selection criteria.
- returnNewDocument: If true, returns the updated document instead of the original document.
- collation: Defines language-specific rules for string comparison.
- arrayFilters: Allows modification of specific elements in an array field.
Return:
- Default: Returns the original document.
- If
returnNewDocument
is true: Returns the updated document.
Examples of findOneAndUpdate() in MongoDB
In the following examples, we are working with:
- Database: gfg
- Collections: student
- Document: Three documents contains the details of the students

Example 1: Update A Document
In this example, we update the first matched document according to the selection criteria(i.e., name:”Nikhil”) by a new document(i.e., {$inc:{score:4}} – the value of the score field is increase by 4) and return the original document:
Query:
db.student.findOneAndUpdate({name:"Nikhil"},{$inc:{score:4}})

Output

Explanation: The document where name: "Nikhil"
will have the score
field incremented by 4. The method will return the original document unless returnNewDocument
is specified.
Example 2: Update Embedded Document
Here, we update the math
field inside the score
embedded document for the student “Ashok” by increasing it by 50.
Query:
db.student.findOneAndUpdate({name:"Ashok"},{$inc:{"score.math":50}})

Output

Explanation: The score.math
field inside the document for “Ashok” will be incremented by 50.
Example 3: Update Document and return the Updated Document
Here, we update the first matched document according to the selection criteria(i.e., name:”Vishal”) by a new document(i.e., {$inc:{score:4}} – the value of the score field is increase by 4) and return the new updated document because we set the value of the returnNewDocument to true.
Query:
db.student.findOneAndUpdate(
{ name: "Vishal" },
{ $inc: { score: 4 } },
{ returnNewDocument: true }
)
Output:
Explanation: The updated document for “Vishal” will be returned, with the score
field incremented by 4.
Example 4: Update Document with Upsert
This example demonstrates the upsert
option. Let’s Update the document for “John” to set the language to “Java” and score to 300, or insert a new document if “John” does not exist.
Query:
db.student.updateOne(
{ name: "John" },
{ $set: { language: "Java", score: 300 } },
{ upsert: true }
);
Output:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1, "upsertedId" : { "_id" : ObjectId("60226a70f19652db63812e8a") } }
Explanation: This query updates the document where the name
is “John” to set the language
to “Java” and score
to 300. If no document matches, it inserts a new document with these values (upsert
).
Example 5: Sort And Update A Document
In this example, we find the student with the highest score and update their language to “JavaScript”
Query:
db.student.find().sort({ "score": -1 }).limit(1).forEach(function(doc) {
db.student.updateOne({ "_id": doc._id }, { $set: { language: "JavaScript" } });
});
Output:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Explanation: This query sorts the documents by score
in descending order and updates the first document found by setting the language
to “JavaScript“.
Best Practices for Using findOneAndUpdate()
- Use Sorting Carefully: When using the
sort
parameter, ensure that it is needed for your use case. Overusing sorting can introduce performance overhead, especially on large datasets.
- Avoid Complex Queries in Loops: If performing
findOneAndUpdate()
in loops, try to minimize the number of queries by optimizing the selection criteria.
- Use
returnNewDocument
Wisely: By default, MongoDB returns the original document. Only set returnNewDocument
to true
if you need the updated document immediately.
Conclusion
The findOneAndUpdate()
method in MongoDB is an invaluable tool for performing atomic updates on a single document. It offers a range of options to fine-tune the behavior of the operation, such as sorting, upserting, and returning the updated document. By understanding its various parameters and options, developers can efficiently update documents based on specific criteria and manage their MongoDB collections effectively.
FAQs
Can findOneAndUpdate()
update multiple documents?
findOneAndUpdate()
updates only the first document that matches the selection criteria.
How can I update a document and retrieve the updated version in MongoDB?
Set the returnNewDocument
option to true
in the method call.
What is the purpose of the upsert
option in findOneAndUpdate()
?
The upsert
option allows you to insert a new document if no document matches the selection criteria.
Can findOneAndUpdate()
update embedded documents?
Yes, findOneAndUpdate()
can update embedded documents within a document
Get IBM Certification and a 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
Master Data Analysis using Excel, SQL, Python & PowerBI with this complete program and also get a 90% refund. What more motivation do you need? Start the challenge right away!
Similar Reads
MongoDB - db.collection.findOneAndUpdate() Method
The MongoDB findOneAndUpdate() method is used to update the first matched document in a collection based on the selection criteria. It offers various options such as sorting, upserting and returning the updated document.In this article, We will learn about MongoDB findOneAndUpdate() in detail by understanding various examples and so on. MongoDB fin
5 min read
Difference Between findOneAndUpdate and findOneAndReplace in MongoDB
In MongoDB, findOneAndUpdate and findOneAndReplace are both update operations that allow us to modify documents in a collection. However, they have distinct behaviors and use cases. In this article, we'll explore the differences between findOneAndUpdate and findOneAndReplace by providing detailed examples. Introduction to Update Operations in Mongo
3 min read
Difference Between findOneAndUpdate and findByIdAndUpdate in MongoDB
In MongoDB, findOneAndUpdate and findByIdAndUpdate are both update operations used to modify documents in a collection. But they are used for different behaviors and use cases. In this article, we'll explore the differences between these two methods by providing detailed examples and outputs to understand the concepts effectively. Introduction to U
3 min read
Mongoose | findOneAndUpdate() Function
The findOneAndUpdate() the method in Mongoose is crucial for atomic updates and ensuring only one modification occurs even in multi-user environments. This atomic nature prevents errors and maintains data integrity. In this article, We will learn about the Mongoose findOneAndUpdate by understanding various concepts related to it in detail with exam
6 min read
Mongoose Queries Model.findOneAndUpdate() Function
The Queries Model.findOneAndUpdate() function of the Mongoose API is used to find and update an existing document with the information mentioned in the "update" object. It finds and updates only the first document that is returned in the filter. Syntax: Model.findOneAndUpdate(conditions, update, options, callback) Parameters: It accepts the followi
3 min read
Mongoose Query.prototype.findOneAndUpdate() API
The Mongoose Query API findOneAndUpdate() method is used to find and update only a single document, from a collection, using the MongoDB query system. Syntax: Query.prototype.findOneAndUpdate(filter, doc, options, callback) Parameters: It accepts the following 4 parameters as mentioned above and described below: filter: It is a mongoose object whic
3 min read
How to Use findOneAndUpdate() in Mongoose?
In the world of MongoDB and Node.js development, Mongoose serves as a powerful tool for interacting with MongoDB databases. One of Mongoose's key methods, findOneAndUpdate, allows developers to easily find a single document in a MongoDB collection, update it, and optionally return either the original or the modified document. In this article, We wi
5 min read
MongoDB insertMany() Method - db.Collection.insertMany()
MongoDB insertMany() method is a powerful tool for inserting multiple documents into a collection in one operation. This method is highly versatile, allowing for both ordered and unordered inserts, and provides options for customizing the write concern. In this article, We will learn about insertMany() in MongoDB by including its syntax, parameters
8 min read
MongoDB updateMany() Method - db.Collection.updateMany()
MongoDB updateMany method is a powerful feature used to update multiple documents in a collection that match a specified filter. This method allows developers to efficiently perform bulk update operations, reducing network overhead and improving performanceIn this comprehensive guide, we will explore the updateMany() method in detail, including its
6 min read
MongoDB Remove() Method - db.Collection.remove()
The MongoDB remove() method allows users to remove documents from a collection based on specific criteria. It is a powerful tool in MongoDB that enables both single and bulk document deletion, offering flexibility in managing your database. It supports various options like removing only one document and specifying a write concernIn this article, we
5 min read