Mongoose Delete Many

Introduction

Deletion is an important part of CRUD operations in any database. In MongoDB, we can delete a single document, or multiple documents, or even we can delete an entire collection. Delete operations can also be performed in mongoose to delete documents in a collection. One such method is deleteMany() method. In this article, we will discuss how to use deleteMany() in mongoose.

deleteMany()

We will perform the deleteMany() method on the details collection.

{ "_id" : ObjectId("5d7d032c5e5d805491ca05d8"), "name" : "John", "age" : 25, "location" : "New York" }
{ "_id" : ObjectId("5d7d03315e5d805491ca05d9"), "name" : "Sam", "age" : 22, "location" : "Texas" }
{ "_id" : ObjectId("5d7d03385e5d805491ca05da"), "name" : "Max", "age" : 21, "location" : "Chicago" }
{ "_id" : ObjectId("5d7d033f5e5d805491ca05db"), "name" : "Lisa", "age" : 26, "location" : "Texas" }
{ "_id" : ObjectId("5d7d03485e5d805491ca05dc"), "name" : "Ronn", "age" : 28, "location" : "Detroit" }

Observe the following function.

router.route("/deleteDocuments").delete(function(req, res) {
  detail.deleteMany({ location: "Chicago" }, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      res.json(result);
    }
  });
});

This function will be invoked when this URL – ‘/deleteDocuments’ will be used. This is a delete operation. In the function, we used the deleteMany() method on the detail collection. The deleteMany() method has two arguments – an object that contains the condition and a callback function. The deleteMany() method will delete all the documents that match the condition. Let’s test it. We will use the postman tool for testing.

Image from Gyazo

The output of the deleteMany() method is an object. This object specifies whether the operation was successful or not.

{
    "n": 1,
    "ok": 1,
    "deletedCount": 1
}

n: This denotes how many documents were matched. ok: This denotes whether the operation was successful or not. deletedCount: This denotes how many documents were deleted.

In our case, the deletedCount is 1 because there is only one document with the location as Chicago.

Let’s delete multiple documents. In the collection, there are multiple documents where the location is Texas. Let’s delete those documents using deleteMany() method.

router.route("/deleteDocuments").delete(function(req, res) {
  detail.deleteMany({ location: "Texas" }, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      res.json(result);
    }
  });
});

Image from Gyazo

The deletedCount is 2. Two documents were deleted.

To delete all the documents from the collection, pass an empty object. Remember, it will delete all the documents only, not the collection.

Conclusion

We hope you can apply the deleteMany Mongoose function to your specific application. If you have any questions about MongoDB please don’t hesitate to contact us. We love to hear about your database issues and come up with solutions together. Thanks for joining another one of our MongoDB tutorials.

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.