Open In App

updateMany() Method in Mongoose

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

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, updateMany() allows you to update many documents at once, providing a more efficient way to handle mass updates.

What is the updateMany() Method in Mongoose?

The updateMany() method in Mongoose is used to update multiple documents in a MongoDB collection that match a specific filter or condition. Unlike updateOne(), which updates only the first document that matches the query, updateMany() applies the update to all documents that meet the specified criteria.

  • Bulk updates: Modifies multiple documents in a single operation.
  • Returns operation result: Provides feedback on how many documents were modified, but does not return the updated documents.
  • Use with upsert: We can use updateMany() with the upsert option to create new documents if none match the condition.

Syntax

Model.updateMany(filter, update, options, callback);

Parameters:

  • filter: The condition or criteria to find the documents that need to be updated.
  • update: The update operation(s) to apply to the matched documents (e.g., $set, $inc).
  • options (optional): Additional options, such as upsert (to create a new document if none match the filter).
  • callback (optional): A callback function that handles the result of the update operation, which contains two parameters: error and writeOpResult.

Return Type:

  • The updateMany() method returns a Query object, which contains details about the operation, including the number of documents matched and modified.

How to Use the updateMany() Method in Mongoose

The updateMany() method is commonly used when you need to update all documents that meet a specific condition. Let’s explore an example to see how it works.

Example 1: Basic Usage

In this example, we will update all users with an age greater than or equal to 5, changing their name to "ABCD".

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 all
// This function has 4 parameters i.e.
// filter, update, options, callback
User.updateMany({age:{$gte:5}},
{name:"ABCD"}, function (err, docs) {
if (err){
console.log(err)
}
else{
console.log("Updated Docs : ", docs);
}
});

Explanation:

  • updateMany() updates all users whose age is greater than or equal to 5, setting their name to "ABCD".
  • The $set operator is used to specify the fields to be updated.
  • The function returns a result that shows how many documents were updated.

Steps to Install Mongoose

Before using the updateMany() method, make sure you have Mongoose installed in your Node.js project.

Step 1: Installation of Mongoose module:

You can visit the link to Install the Mongoose module. You can install this package by using this command.

npm install mongoose

Step 2: Verify Mongoose Installation

After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

The project structure will look like this: project structure

Your package.json should now show Mongoose as a dependency:

"dependencies": {
"mongoose": "^7.6.5"
}

Below is the sample data in the database before the function is executed, We can use any GUI tool or terminal to see the database, like we have used Robo3T GUI tool as shown below: Database

Step 3: Run the Application

Once you've set up your Node.js application, run the following command to execute the script:

node index.js

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

So this is how you can use the mongoose updateMany() function which is the same as update(), except MongoDB will update all documents that match the filter. Used when the user wants to update all documents according to the condition.

Conclusion

The updateMany() method in Mongoose is a powerful tool for bulk updating documents in MongoDB. It allows us to modify multiple documents that match a given condition, providing a simple way to apply changes to large datasets. While it doesn’t return the updated documents, it returns valuable information about how many documents were affected. For scenarios where you need to update a single document, consider using updateOne().


Next Article

Similar Reads