How to Drop a MongoDB Collection with NodeJS

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

Introduction

If you’re working with MongoDB through NodeJS and Javascript and you need to know how to drop a collection, we’ll show how to do it in this article. Be aware that this will delete all documents from the collection so be sure you really want to do the collection before proceeding with the steps. We hope you can get all the code snippets that you need to accomplish this task in your specific application.

If you don’t need a play by play and just want to see the code you can scroll to the buttom to the Just Show Me The Code section.

Prerequisites

  • You should have MongoDB installed and running.
  • You should have NodeJS and npm installed.
  • You shou have installed the mongodb module from npm.
    • npm install mongodb

Goal

In an effort to demonstrate how to drop a collection, we will drop our demoCollection which already exists in our demoDatabase.

The Gist

If you’re using NodeJS and you haven’t established a connection to MongoDB and selected a database then here’s the full code you’ll need. Let’s take a look at the code first and then dissect it afterwards.

File: deleteCollection.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

// Connect to Mongo
MongoClient.connect(url, function (error, client) {
    if (error) throw error;

    // Select database
    var dbo = client.db("demoDatabase");

    // Drop the collection
    dbo.collection("demoCollection").drop(function (err, result) {
        if (err) throw err;
        if (result) console.log("Collection successfully deleted.");
        c.close();
    });
});

Let’s start explaining the code:

1
var MongoClient = require('mongodb').MongoClient;
  • First we require the mongodb module which you should’ve installed using npm. These are the drivers that give us the tools to interact with MongoDB. We assign it to the MongoClient varialbe which we’ll use everywhere to interact with MongoDB.
  • Next we declare the url to connect to MongoDB. We’re using localhost and standard port but adapt these to you’re specific situation.
1
var url = "mongodb://localhost:27017/";

Next we use the connect() method to establish a connection to MongoDB and give it a callback to what we want done when the connection has been established. As the comments explain, we then select a database with `client.db() and then remove the collection with the drop() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
MongoClient.connect(url, function (error, client) {
    if (error) throw error;

    // Select database
    var dbo = client.db("demoDatabase");

    // Drop the collection
    dbo.collection("demoCollection").drop(function (err, result) {
        if (err) throw err;
        if (result) console.log("Collection successfully deleted.");
        client.close();
    });
});

When you run the app with node you should see the successful log message when you run the script. Our script is called deleteCollection.js so this is what the call looks like.

1
2
$ node deleteCollection.js
Collection successfully deleted.

Learn More

To learn more about the process the MongoDB has excellent documentation. We encourage you to look into each function we used including: connect(), drop(), and db().

Conclusion

In this tutorial we showed you how to drop a collection in Javascript using the mongodb library which can be found on npm. We connected to our MongoDB through the connect() call and this gives us the ability to interact with MongoDB. Then we selected the database we wanted to act on and dropped the collection. We hope you were able to find the snippets of code you needed for your specific application.

Just Show Me The Code

If you just want to see all the code:

File: deleteCollection.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

// Connect to Mongo
MongoClient.connect(url, function (error, client) {
    if (error) throw error;

    // Select database
    var dbo = client.db("demoDatabase");

    // Drop the collection
    dbo.collection("demoCollection").drop(function (err, result) {
        if (err) throw err;
        if (result) console.log("Collection successfully deleted.");
        c.close();
    });
});

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.