Mongoose updateOne() Method
Last Updated :
25 Mar, 2025
In Mongoose, the updateOne()
method is an essential function for modifying documents in a MongoDB collection. It updates the first document that matches a specified filter, providing a simple and efficient way to update data. This function is similar to the update()
method but is limited to updating only one document and does not support the multi
or overwrite
options.
What is updateOne()
Method in Mongoose?
The updateOne()
method in Mongoose is used to update a single document that matches a specified filter condition. It performs the update operation on the first document that matches the filter and does not return the updated document itself. Instead, it provides metadata about the update, such as how many documents were affected.
Unlike the update()
method, updateOne()
does not support the multi
or overwrite
options, making it more precise for scenarios where you want to update just one document.
Key Points:
- Updates the first matching document based on the filter.
- Does not return the updated document; instead, it provides information about the update operation.
- Does not support
multi
or overwrite
options.
Syntax:
Model.updateOne(filter, update, options, callback )
Parameters:
It accepts the following 4 parameters as mentioned above and described below:
- filter: It is a mongoose object which identifies the existing document to update.
- update: It is a mongoose object which is the document that will update the data in the existing document.
- options (optional): It is an optional mongoose object which is derived from Query.prototype.setOptions().
- callback (optional): It is a callback function that accepts 2 parameters: error and writeOpResult.
Return type:
- The
updateOne()
method returns a Query object. It provides metadata about the update operation, including the number of documents matched and modified.
How to Use the updateOne()
Method in Mongoose
Now, let’s go through some practical examples to demonstrate how the updateOne()
method works in Mongoose.
Example 1: Basic Usage
This example demonstrates using Mongoose's updateOne() method to update the first document in the "User" collection where the age is greater than or equal to 5, changing the user's name to "ABCD". The method takes a filter, update, options, and a callback function to handle the result or any errors.
// Filename - index.js
const mongoose = require('mongoose');
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
});
// User model
const User = mongoose.model('User', {
name: { type: String },
age: { type: Number }
});
// Find all documents matching the condition
// (age >= 5) and update first document
// This function has 4 parameters i.e.
// filter, update, options, callback
User.updateOne({age:{$gte:5}},
{name:"ABCD"}, function (err, docs) {
if (err){
console.log(err)
}
else{
console.log("Updated Docs : ", docs);
}
});
Explanation:
updateOne()
updates the first document where the age
is greater than or equal to 5.$set
is used to set the name
field to "ABCD"
Steps to Install Mongoose Module
Before using the updateOne()
function, ensure that you have Mongoose installed in your Node.js project. Follow these steps:
Step 1: You can install this package by using this command.
npm install mongoose
Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.
npm version mongoose
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"mongoose": "^7.6.5",
}
Steps to run the program: Run the following command in the terminal
node index.js
Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database, like we have used Robo3T GUI tool as shown below: 
Console Output:
After the function is executed, You can see the database as shown below: 
So this is how you can use the mongoose updateOne() function which is used to update the first document that matches the condition. This function is the same as the update(), except it does not support the multi or overwrite options.
Conclusion
The updateOne()
method in Mongoose is an efficient way to update a single document in a MongoDB collection. It is ideal for cases where you need to modify one document based on specific conditions, without needing to retrieve the updated document itself. For scenarios that require updating multiple documents or returning the updated document, consider using updateMany()
or findOneAndUpdate()
instead. By understanding how to use updateOne()
, you can optimize your database operations in Mongoose and make your applications more efficient.
Similar Reads
MongoDB - updateOne() Method
MongoDB's updateOne() method provides a powerful way to update a single document in a collection based on specified criteria. This method is particularly useful when Accuracy is needed in modifying specific documents without affecting others.In this article, We will learn about MongoDBâs updateOne()
6 min read
updateMany() Method in Mongoose
In Mongoose, the updateMany() method is a powerful tool for performing bulk updates in MongoDB. It updates multiple documents that match a specified condition, applying the changes to all the matched documents in a single operation. Unlike updateOne(), which updates only the first matching document,
4 min read
MongoDB - Update() Method
MongoDB update operations allow us to modify documents in a collection. These operations can update a single document or multiple documents based on specified criteria. MongoDB offers various update operators to perform specific actions like setting a value, incrementing a value or updating elements
7 min read
Mongoose Populate() Method
The populate() method in Mongoose is used to automatically replace a field in a document with the actual data from a related document. It simplifies handling referenced documents and helps replace ObjectIds with the actual data from related collections. This article explores the Mongoose populate()
5 min read
Mongoose save() Method
The Mongoose save() Method is a fundamental method in Mongoose used to persist data in a MongoDB database. It allows us to create new documents or update existing ones, ensuring that data is validated and saved correctly according to the schema definition. This article explains how to use the save()
4 min read
Mongoose Document Model.updateOne() API
The Model.updateOne() method of the Mongoose API is used to update documents in a collection. This method will update the very first document that will match the filter irrespective of the value of the multi-option. Model.updateOne() method accepts four parameters: filter: It is an object to filter
3 min read
Mongoose prototype.$set() Method
In Mongoose, the prototype.$set() method is a crucial function that allows developers to update specific fields of a document in a MongoDB collection. This method enables atomic modification of field values within a document, offering precise control over your MongoDB data. This article explains how
5 min read
Mongoose update() Function
In Mongoose, the update() function is used to modify a document in a MongoDB collection based on a given filter. It updates a document but does not return the updated document itself. This function is part of Mongooseâs Query API and is a powerful tool for managing data in your Node.js applications.
5 min read
Mongoose Document Model.updateMany() API
The Mongoose Document API Â Model.updateMany() method of the Mongoose API is used on the Document model. It allows updating more than one document in one go. Using this method we can update multiple documents that match the filter condition in one execution. Let us understand the updateMany() method
3 min read
Mongoose Queries Model.updateOne() Function
The Queries Model.updateOne() function of the Mongoose API is used to update an existing document with the information mentioned in the "update" object. It updates only the first document that is returned in the filter. Syntax: Model.updateOne(filter, update, options, callback ) Parameters: It accep
3 min read