![]() |
VOOZH | about |
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.
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.
updateMany() with the upsert option to create new documents if none match the condition.Model.updateMany(filter, update, options, callback);
$set, $inc).upsert (to create a new document if none match the filter).error and writeOpResult.updateMany() method returns a Query object, which contains details about the operation, including the number of documents matched and modified.updateMany() Method in MongooseThe 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.
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);
}
});
updateMany() updates all users whose age is greater than or equal to 5, setting their name to "ABCD".$set operator is used to specify the fields to be updated.Before using the updateMany() method, make sure you have Mongoose installed in your Node.js project.
You can visit the link to Install the Mongoose module. You can install this package by using this command.
npm install mongooseAfter installing the mongoose module, you can check your mongoose version in the command prompt using the command.
npm version mongooseThe 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
Once you've set up your Node.js application, run the following command to execute the script:
node index.jsπ ImageAfter 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.
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().