How to Use the Mongoose deleteMany Method

Introduction

Apart from deleteOne(), there is one another widely used method in mongoose to perform delete operation. This method is known as deleteMany(), and it is used to delete multiple documents. It is very simple and easy to use the method. In this article, we will discuss how to use the mongoose deleteMany() method to delete multiple documents.

We will use the deleteMany() to delete documents from the kennels collection.

{ "_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("5dc3e76458a113d0ebf3d488"), "name" : "Tyke", "age" : 4, "breed" : "Pitbull" }
{ "_id" : ObjectId("5dc3e77a58a113d0ebf3d489"), "name" : "Rambo", "age" : 3, "breed" : "Labrador" }

For performing HTTP endpoint testing, we will use the postman tool. You can download the postman tool from www.getpostman.com.

deleteMany() method

The following route handler will be invoked when the endpoint ‘/delete’ will be executed.

router.route("/delete").delete(function(req, res) {});

The deleteMany() method has two mandatory parameters – the query and a callback function. The query matches the documents that will be deleted. If we pass an empty object as a query, then all the documents in the collection will be deleted. The callback function has two parameters – an error (if any occurs) and the result.

router.route("/delete").delete(function(req, res) {
  kennels.deleteMany({ breed: "Labrador" }, function(err, result) {
    if (err) {
      res.send(err);
    } else {
      res.send(result);
    }
  });
});

As a query, we passed the following object.

{"breed" : "Labrador"}

All the documents in the kennels collection where the value of the breed field is “Labrador”, will be deleted. Let’s execute the route using the postman tool.

Image from Gyazo

The deleteMany() method returns an object that contains three fields:

  1. n – number of matched documents
  2. ok – 1 if the operation was successful
  3. deletedCount – number of deleted documents

So, in our result, the value of n is 2, this means the query matched two documents. The value of deletedCount is also 2, this means all the matched documents were deleted successfully. Let’s verify using the mongo shell.

> 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("5dc3e76458a113d0ebf3d488"), "name" : "Tyke", "age" : 4, "breed" : "Pitbull" }
>

Yes! each document where the value of the breed field is “Labrador” is deleted. This is how the deleteMany() method works.

Conclusion

Thanks for joining us for another Object Rocket tutorial. We hope you’re able to apply what you’ve learned about using the mongoose deleteMany method. If you have any questions or need database help please reach out to us.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.