Mongodb-node.js Tutorial Series

Drop mongodb collection using node.js

MongoDB-Nodejs tutorial | Perform mongodb operations using node.js



Overview

In this part of the mongodb operations using node.js tutorial series , we will learn about how we can drop a mongodb collection using node.js.

Let's Start !!
Step 1 - Include Package

We will start by including mongodb npm package as shown below :

												
var mongo = require('mongodb');
												
											

Step-2 : Establish Connection

Establish a connection between the mongoDb database and our node.js app using the following :

												
var new_db = "mongodb://localhost:27017/demo_db"
												
											

  • demo_db is the name of the database. You can change it in accordance with your database name.
  • 27017 is the port where mongoDb is running.
  • Localhost i.e. 127.0.0.1 is the local IP.

Step-3 : Drop collection

There are 2 methods with which we can drop a collection in mongodb which are expained with example below.

  1. drop : This is the first method which we are going to discuss. It's syntax is :
    													
    db.collection("NAME_OF_THE_COLLECTION").drop( (CALLBACK_FUNCTION) => {});
    													
    												

    An example is given below :

    													
    //drop.js
    var mongo = require('mongodb');
    var new_db = "mongodb://localhost:27017/demo_db"
    
    //Connecting to the database
    mongo.connect(new_db ,(error , db) => {
    	if (error){
    		throw error;
    	}
    	//ACCESSING A COLLECTION IN MONGODB USING NODE.JS
    	db.collection("new").drop( (err , collection) => {
    		if(err) throw err;
    		if(collection) console.log("Dropped successfully");
    		db.close();
    	});
    });
    													
    												

    You can run the above code using the following command :
    													
    >node drop.js
    Dropped successfully
    
    													
    												

  2. dropCollection() : This is another way of deleting a mongodb collection. It's syntax is :
    													
    db.dropCollection("NAME OF THE COLLECTION" , (CALLBACK FUNCTION) => {});
    													
    												

    An example is given below :

    													
    //dropcollection.js
    var mongo = require('mongodb');
    var new_db = "mongodb://localhost:27017/demo_db"
    
    mongo.connect(new_db ,(error , db) => {
    	if (error){
    		throw error;
    	}
    	
    	//Accessing the collection
    	db.dropCollection("details" , (err , collection) => {
    		if(err) throw err;
    		
    		if(collection) console.log("Dropped successfully");
    		
    		db.close();
    	});
    });
    													
    												

    You can run the above code using the following command :
    													
    >node dropcollection.js
    Dropped successfully
    
    													
    												

What we learned

In this article we learned about

  1. Including monogdb npm package in your app.
  2. Establishing a connection between mongodb database and node.js application.
  3. drop() method in mongodb to delete a collection using node.js
  4. dropCollection() in mongodb to delete a collection using node.js