Open In App

Mongoose updateOne() Method

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

NodeProj

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: Database

Console Output:

After the function is executed, You can see the database as shown below: new Database

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.


Next Article

Similar Reads