Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
If you are using Mongoose to interact with MongoDB, this article will guide how you can delete the Document using Mongoose by providing you with various options for the same.
We can perform deletion of all the documents in the collection, delete a single document by the id, or delete the documents that specify a condition. We can pick the best option that matches our needs. Let's see the different methods provided by Mongoose.
To Delete many Documents.
In the article, we will be using a model called CodingNinjas, for all the examples.
If you want to delete many documents using multiple conditions, we can use the Model.deleteMany() command. Let's see the function syntax and the definition,
Let's take an example of a Ninja and use that in our function to delete all the documents.
CodingNinjas.deleteMany({ brand: 'Ninja' }, function (err) {
if(err) console.log(err);
console.log("Deleted all the documents.");
});
In the above example, we can see that we have used the condition parameter to target the documents we want to delete; we can use the callback function. Using the above function, we can delete all the documents with the brand name Ninja present in the CodingNinja model.
Delete one Document
Mongoose provides a function Model.deleteOne(), which is used to delete the first Document that matches the condition provided by the user. Let's see the function syntax and the definition.
Model.deleteOne()
Parameters
conditions (Object)
[callback] (Function)
Let's take an example considering the Ninja to delete the first Document with a brand equal to Ninja. We will use a callback function to log if it has successfully deleted the Document.
CodingNinjas.deleteOne({ brand: 'Ninja' }, function (err) {
if(err) console.log(err);
console.log("Deleted the first document.");
});
Find one and Delete
The following method is similar to the one we discussed above, Model.deleteOne(), but it provides us with a lot more options. The method which is available to find one and delete we can use Model.findOneAndDelete(). Let's see the function syntax and the definition,
Model.findOneAndDelete()
Parameters
conditions (Object)
[options] (Object)
[options.strict] (Boolean|String)
[callback] (Function)
Let’s take an example using the brand name Ninja,
CodingNinjas.findOneAndDelete({ brand: 'Ninja' }, function (err) {
if(err) console.log(err);
console.log("Deletion Successful using the findoneandelete method");
});
Delete by ID
The last method available is delete by id, using which we can delete a document using an id.
Let’s see the function definition and the syntax,
Model.findByIdAndDelete()
Parameters
id (Object|Number|String) value of _id to query by
[options] (Object) optional see Query.prototype.setOptions()
[options.strict] (Boolean|String) overwrites the schema's strict mode option
[callback] (Function)
Let’s discuss an example to understand better using the id.
CodingNinjas.findByIdAndDelete(id, function (err) {
if(err) console.log(err);
console.log("Deletion is successful.");
});
We have discussed the Mongoose syntax and functionality for deleting documents in MongoDB.
What are the available functions for deletion? Ans: The available functions for deletion are: → Model.deleteMany() → Model.deleteOne() → Model.findOneAndDelete() → Model.findByIdAndDelete()
What is the use of the Model.deleteMany() method? Ans: To delete many documents based on multiple conditions, we can use the Model.deleteMany() command.
Why is the use of the Model.deleteOne() method? Ans: The method Model.deleteOne() is used to delete the first Document that matches the condition provided by the user.
Key Takeaways
Hey everyone, so let’s brief out the article. Let’s discuss in brief whatever we have discussed here.
We have seen that we can perform deletion of all the documents in the collection. We can delete a single document by the id or delete the documents that specify a condition. We can pick the best option that matches our needs.
We have discussed each of the available methods to delete a document with conditions in detail, along with each example.
The article then covers the most frequently asked questions from the developers about the deletion of a document using Mongoose.
Isn’t Web Development engaging? Building new websites and using amazing animations and different APIs, don’t be scared if you cannot grasp the hold of this vast development. We have the perfect web development course for you to make you stand out from your fellow developers.