Mongoose Delete
Introduction
Deleting data from a database is a critical operation. Delete operations should be performed carefully. There is no margin for error. Mongoose provides two delete methods for deleting documents from a MongoDB collection. They are deleteOne() and deleteMany(). In this article, we will discuss these methods for deleting documents in mongoose.
We will delete data from the details collection.
1 2 3 4 5 6 | { "_id" : ObjectId("5d9a3968631be4c33cbbb144"), "name" : "John", "age" : 21 } { "_id" : ObjectId("5d9a396a631be4c33cbbb145"), "name" : "Max", "age" : 23 } { "_id" : ObjectId("5d9a396e631be4c33cbbb146"), "name" : "Lisa", "age" : 27 } { "_id" : ObjectId("5d9a3972631be4c33cbbb147"), "name" : "Sam", "age" : 21 } { "_id" : ObjectId("5d9a3974631be4c33cbbb148"), "name" : "Ronn", "age" : 24 } { "_id" : ObjectId("5d9a397d631be4c33cbbb149"), "name" : "Riley", "age" : 29 } |
We will be using the following route for explaining.
1 | router.route("/delete").post(function(req, res) {}); |
Also, we will use the postman tool for testing the route. You can download this tool from www.getpostman.com.
deleteOne()
The deleteOne() method is used to delete a single document from a collection. We can pass a filter condition to this method, and it will delete the first matching document. But if no condition is given, the deleteOne() method will delete the very first document of the collection. Let’s try this.
1 2 3 4 5 6 7 8 9 | router.route("/delete").post(function(req, res) { detail.deleteOne({}, function(err, result) { if (err) { console.log(err); } else { console.log(result); } }); }); |
The deleteOne() method has two parameters – an object that contains the condition (it can be empty object also) and the callback function. The callback function also has two parameters – an error (if any occurs) and the returning value. In the body of this function, we are displaying the returning value in the console.
Let’s execute the route and see what is the output in the console.
The deleteOne() method returns an object. Let’s discuss what the returning objects say.
n:1 – This means, only one document was matched. ok:1 – This means, the operation was successful. deletedCount:1 – This means, one document was deleted.
The returning object sums it all. This deleteOne() method deleted the very first document because there was no condition.
Let’s delete a specific document from this collection.
1 2 3 4 5 6 7 8 9 | router.route("/delete").post(function(req, res) { detail.deleteOne({ name: "Ronn" }, function(err, result) { if (err) { console.log(err); } else { console.log(result); } }); }); |
This time we passed a condition ({ “name” : “Ronn”}) to the deleteOne() method. This will delete the document where the name is “Ronn”. But remember, it will delete only the first matched document.
Let’s have a look at the output.
As stated along, the deleteOne() method only deletes a single document. There is another method for deleting multiple documents and we will discuss it next.
deleteMany()
The deleteMany() method is used to delete multiple methods from a collection. Similar to the deleteOne() method, the deleteMany() method also has two parameters – an object for condition and callback function. The callback function also has two parameters – an error (if any) and returning value.
Let’s delete multiple methods by using the deleteMany() method.
1 2 3 4 5 6 7 8 9 | router.route("/delete").post(function(req, res) { detail.deleteMany({ age: 21 }, function(err, result) { if (err) { console.log(err); } else { console.log(result); } }); }); |
Here, we passed an object ({“age” : 21}) to the deleteMany() method. This means every document where age is 21 should be deleted. Let’s have a look at the output.
The value of n is 2 and the deletedCount is also 2. This means, two documents were matched and those were deleted. This is how deleteMany() works. But what would happen when an empty object is passed to this method. Let’s try.
1 2 3 4 5 6 7 8 9 | router.route("/delete").post(function(req, res) { detail.deleteMany({}, function(err, result) { if (err) { console.log(err); } else { console.log(result); } }); }); |
There were four documents remaining in the collection and all of them are deleted. This happens when an empty object is passed to the deleteMany() method. It deletes all the documents from a collection.
Conclusion
So we have two methods for deleting in mongoose. The deleteOne() method for deleting a single document and the deleteMany() method for deleting multiple documents. We can choose a method according to our requirements.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started