![]() |
VOOZH | about |
The deleteMany() function is employed to remove all documents meeting specified conditions from a collection. Unlike the remove() function, deleteMany() deletes all matching documents without considering the single option. This method is essential for Node.js developers working with Mongoose, as it allows us to perform bulk deletions based on specific conditions, simplifying data management in MongoDB.
Mongooseโs deleteMany() function is a powerful method to remove multiple documents from a MongoDB collection based on specified conditions. This method is often used in Node.js applications where we need to delete several records that match a given filter. Unlike the remove() function, deleteMany() allows us to delete all matching documents at once without considering the single document option.
deleteMany() is its ability to remove multiple documents based on the filter you provide.find(), deleteMany() does not return the deleted documents, but rather, it returns a result object with information about the operation.Syntax:
Model.deleteMany()
writeConcern.The Model.deleteMany() function returns a promise. The result contains an array of objects having details of documents inserted in the database.
Step 1: You can visit the link Install Mongoose module. You can install this package by using this command.
npm install mongooseStep 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.
npm version mongooseProject Structure:
๐ NodeProjThe updated dependencies in package.json file will look like:
"dependencies": {
"mongoose": "^7.6.5",
}
Letโs look at a practical example that demonstrates how to use the deleteMany() function to delete multiple documents from a MongoDB collection.
const mongoose = require('mongoose');
// Database connection
mongoose.connect(
'mongodb://127.0.0.1:27017/geeksforgeeks',
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// User model
const User = mongoose.model(
'User',
{
name: { type: String },
age: { type: Number }
});
// Function call
// Deleting all users whose age >= 15
User.deleteMany(
{
age: { $gte: 15 }
}).then(
function () {
// Success
console.log("Data deleted");
}).catch(
function (error) {
// Failure
console.log(error);
});
Steps to run the program:
node index.jsOutput: Below is the sample data in the database before the deleteMany() function is executed:๐ Database after delete command
Console Output:
After running above command, you can see the data is deleted from the database. ๐ Database after delete command
The deleteMany() function in Mongoose is an efficient way to delete multiple documents from a collection in MongoDB. It is especially useful for bulk delete operations, where we need to remove documents based on specific conditions. With its simple syntax, promise-based execution, and ease of use, deleteMany() is an essential tool in Mongoose for managing our data. By using this method, we can easily manage data removal in your applications, making our MongoDB operations more efficient and powerful.