Mongoose deleteOne
Introduction
Mongoose provides many methods to delete documents from a collection. One of these methods is deleteOne(). People often confuse deleteOne() with delete(). But there is no method known as delete() in mongoose. The method that is used to delete a single document from a MongoDB collection is the deleteOne() method. In this article, we will discuss how to use Mongoose deleteOne() method.
We will perform the deleteOne() method on the kennels collection.
1 2 3 4 5 | { "_id" : ObjectId("5db6b24830f133b65dbbe457"), "name" : "Mini", "age" : 5, "breed" : "Labrador" } { "_id" : ObjectId("5db6b25b30f133b65dbbe458"), "name" : "Max", "age" : 2, "breed" : "German Shephard" } { "_id" : ObjectId("5db6b26730f133b65dbbe459"), "name" : "Spike", "age" : 3, "breed" : "Great Dane" } { "_id" : ObjectId("5db6b28d30f133b65dbbe45b"), "name" : "Romeo", "age" : 2, "breed" : "Labrador" } { "_id" : ObjectId("5dc3bde558a113d0ebf3d487"), "name" : "Tyke", "age" : 4, "breed" : "Pitbull" } |
For performing HTTP endpoint testing, we will use the postman tool. You can download the postman tool from www.getpostman.com.
deleteOne() method
As the name suggests, the deleteOne() deletes a single document from a collection. So let’s understand how to use the deleteOne() method.
The following route handler will be invoked when the endpoint ‘/delete’ will be executed.
1 | router.route("/delete").delete(function(req, res) {}); |
Let’s delete the document from the kennels collection where the breed is Pitbull.
1 2 3 4 5 6 7 | kennels.deleteOne({ breed: "Pitbull" }, function(err, result) { if (err) { res.send(err); } else { res.send(result); } }); |
The deleteOne() method has two mandatory parameters – the query and a callback function. The query will match the document that is to be deleted. So to delete the document where the breed is Pitbull, we passed the following query.
1 | {"breed" : "Pitbull"} |
The callback function has two parameters – an error (if any occurs) and the result. Let’s execute this route using the postman tool and see what happens.
The deleteOne() method returns an object containing three fields.
- n – number of matched documents
- ok – 1 if the operation was successful
- deletedCount – number of documents deletedCount
So in our result, n is 1, and it should be 1 because the deleteOne() method only matches a single document. The deletedCount is also 1, this means 1 document was deleted successfully. Let’s verify using the mongo shell.
1 2 3 4 5 6 | > db.kennels.find() { "_id" : ObjectId("5db6b24830f133b65dbbe457"), "name" : "Mini", "age" : 5, "breed" : "Labrador" } { "_id" : ObjectId("5db6b25b30f133b65dbbe458"), "name" : "Max", "age" : 2, "breed" : "German Shephard" } { "_id" : ObjectId("5db6b26730f133b65dbbe459"), "name" : "Spike", "age" : 3, "breed" : "Great Dane" } { "_id" : ObjectId("5db6b28d30f133b65dbbe45b"), "name" : "Romeo", "age" : 2, "breed" : "Labrador" } > |
The document where the value of the breed field was “Pitbull” is deleted.
What if there are two documents with the same value of the specified field? For example, in the kennels collection, there are two documents where the breed is Labrador. What do you think will happen if we use the deleteOne() method to delete the document where the breed is Labrador? Let’s pass the following query to the deleteOne and see what happens.
1 | {"breed" : "Labrador"} |
It deleted only one document. But we had two documents where the value of the breed fields is Labrador. Let’s check what happened using the mongo shell.
1 2 3 4 5 | > db.kennels.find() { "_id" : ObjectId("5db6b25b30f133b65dbbe458"), "name" : "Max", "age" : 2, "breed" : "German Shephard" } { "_id" : ObjectId("5db6b26730f133b65dbbe459"), "name" : "Spike", "age" : 3, "breed" : "Great Dane" } { "_id" : ObjectId("5db6b28d30f133b65dbbe45b"), "name" : "Romeo", "age" : 2, "breed" : "Labrador" } > |
The deleteOne() method deleted the first document that matched the query. Yes! this is how the deleteOne() method works. It does not matter how many documents can match the query, but the deleteOne() method will delete only the first matched document.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started