Mongoose Drop Collection if Exists

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Mongoose provides various methods that can be used for delete operation. We can delete a single document or multiple documents from a collection. We can also delete an entire MongoDB database or an entire collection. Deleting an entire collection should be done carefully. There can be a huge amount of data in a collection, and once a collection is deleted, it is lost permanently. Mongoose provides the dropCollection method to delete a MongoDB collection. In this article, we will discuss how to use Mongoose to drop a collection if it exists.

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

The dropCollection() method

We will delete the details collection using the dropCollection() method. But first, let’s check how the details collection looks like.

Image from Gyazo

So there are few documents in the details collection. Let’s start by creating a route handler.

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

This route handler will be invoked when the endpoint, ‘deleteCollection’ will be executed.

We can invoke the dropCollection() method using the following syntax.

1
mongoose.connection.db.dropCollection();

Remember, mongoose should be connected with that database where the collection we want to delete exists.

The dropCollection() method has two parameters – the name of the collection and a callback function. The callback function, in turn, has two parameters – an error(if any occurs) and the result.

1
2
3
4
5
6
7
8
9
router.route("/deleteCollection").delete(function(req, res) {
  mongoose.connection.db.dropCollection("details", function(err, result) {
    if (err) {
      res.send(err);
    } else {
      res.send(result);
    }
  });
});

Let’s execute this route using the postman tool and see what happens.

Image from Gyazo

The dropCollection() methods return true after deleting the collection successfully. Let’s check if the details were deleted or not using the mongo shell.

Image from Gyazo

The find() method returns nothing. This means the details collection was deleted successfully.

Now, there are no details collection in the database. What will happens if we again try to execute the route and invoke the dropCollection() method? Let’s see.

It will throw an error specifying, “ns not found”. So the dropCollection() works fine when the collection exists but throws an error if there is no collection of the name specified.

Conclusion

So the dropCollection() method is a useful method that is used to delete an entire collection from a MongoDB database. But it should be used carefully as it deletes all the documents and indexes present in the collection.

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.