![]() |
VOOZH | about |
The findByIdAndDelete() function in Mongoose is a commonly used method to find and remove a document by its unique ID in MongoDB. This method simplifies the process of deleting documents, making it an essential tool for Node.js developers working with MongoDB. Whether you're managing user data, posts, or other collections, this method provides an efficient way to handle deletions.
findByIdAndDelete() Method?findByIdAndDelete() is a Mongoose method used to search for a document by its _id and remove it from the collection. It not only deletes the document but also returns the deleted document (if found), providing useful feedback for debugging and logging. Unlike older methods like remove(), findByIdAndDelete() is preferred for its clarity and simplicity. It’s a part of Mongoose’s more modern and consistent API for document removal operations.
Model.findByIdAndDelete(id, options, callback);
Parameters:
_id field).lean, select).Follow these steps to use the findByIdAndDelete() method in Mongoose to delete documents based on their unique _id:
You can visit the link to Install mongoose module. Run the following command in your terminal. This will install the latest version of Mongoose.
npm install mongooseAfter installing mongoose module, we can check your mongoose version in command prompt using the command.
npm version mongooseThis ensures that Mongoose is installed correctly and shows the version in the command prompt.
index.js).index.js file to use findByIdAndDelete(). Here’s an example demonstrating how to use the findByIdAndDelete() function to delete a document from the database:
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 }
});
// Find a document whose
// id=5ebadc45a99bde77b2efb20e and remove it
var id = '5ebadc45a99bde77b2efb20e';
User.findByIdAndDelete(id, function (err, docs) {
if (err){
console.log(err)
}
else{
console.log("Deleted : ", docs);
}
});
Explanation:
mongoose.connect().User model with fields name and age.findByIdAndDelete() function is called with a specific id to find and delete the document matching that ID.Run the File: Open your terminal and navigate to the folder where index.js is located. Run the following command to execute the file:
node index.jsThe project structure will look like this: 👁 project structure
Make sure you have installed mongoose module using following command:
npm install mongooseBelow 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
Run index.js file using below command. After running the above command, the document with the specified ID will be deleted from the database.
node index.js👁 ImageAfter the function is executed, You can see in the database that the particular user is deleted as shown below: 👁 new Database
findByIdAndDelete()?findByIdAndDelete() provides a straightforward, clean API for deleting documents based on their unique _id.remove().The findByIdAndDelete() method in Mongoose is a simple yet powerful tool to delete a document by its ID from MongoDB. Its clear syntax and modern implementation make it a preferred choice over older methods. Whether you're building user management features or cleaning up your database, findByIdAndDelete() offers an efficient and easy-to-use solution for deleting documents in Mongoose. By following the steps outlined above, you can easily integrate this method into your Node.js and Mongoose applications.