Drop the collection in Mongodb database using Node.js


What do we intend to make ?

In this Chapter , we will learn about deleting the collection in mongodb using nodejs

Let's Start !

Step - 1 : Including Packages
We will start by requiring the package. We are using the following package in our application :

		        

var mongo = require('mongodb');
                
	            


Step - 2 : Establish Connection
Now let's establish a connection between the mongoDb Database and our node.js application.

		        

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



                
	            

  • demo_db is the name of the database. You can change it as per your wish.
  • 27017 is the port where our mongoDb is running.
  • Localhost i.e. 127.0.0.1 is the local IP.

Step - 3 : DROP the collection
There are 2 ways to drop the collection in mongodb using nodejs which are as follows :

  1. drop : This is the first method which we are going to discuss. It's syntax and example are given below.
    Syntax : The syntax for drop() is :                
    		        
    db.collection("NAME_OF_THE_COLLECTION").drop( (CALLBACK_FUNCTION) => {
    				
    	            

    Let's understand with the help of an example :

    		        
    //deleteCollection-mongodb-nodejs.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("Table Deleted successfully");
    		db.close();
    	});
    });
     
    	            

    You can run the above code using the following command :

    		        
    
    D:\nj-learn-mongo>node deleteCollection-mongodb-nodejs.js
    
    				
    	            

    The output of the above code is :

    		        
    
    D:\nj-learn-mongo>node deleteCollection-mongodb-nodejs.js
    Table Deleted successfully
    		
    	            


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

    Let's Understand it with the help of an example :

    		        
    //dropcollection-mongodb-nodejs.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("Table Deleted successfully");
    		
    		db.close();
    	});
    });
              
    	            

    You can run the above code using the following command :

    		        
    
    D:\nj-learn-mongo>node dropcollection-mongodb-nodejs.js
    
    				
    	            

    The output of the above code is :

    		        
    
    D:\nj-learn-mongo>node dropcollection-mongodb-nodejs.js
    Table Deleted successfully