Node.js – Delete Collection in MongoDB

We can delete a MongoDB Collection using Node.js program using remove() method of MongoDB Collection.

In this Node.js Tutorial, we shall learn to Delete Collection in MongoDB from Node.js Application, using db.collection.remove() method, with an example.

Steps to Delete Collection in MongoDB via Node.js

Following is a step by step guide with an example to delete a collection in MongoDB from Node.js Application.

Step 1: Start MongoDB Service.

Run the following command to start MongoDB Service.

sudo service mongod start

Step 2: Get the base URL to MongoDB Service.

A simple hack to know the base url of MongoDB Service is to Open a Terminal and run Mongo Shell.

arjun@nodejs:~$ mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings: 
2017-10-29T18:15:36.110+0530 I STORAGE  [initandlisten] 

While the Mongo Shell starts up, it echoes back the base url of MongoDB.

mongodb://127.0.0.1:27017

Step 3: Prepare the complete URL.

Append the Database name you want to connect to (say newdb), to the base URL.

mongodb://127.0.0.1:27017/newdb

Step 4: Create a MongoClient.

</>
Copy
var MongoClient = require('mongodb').MongoClient;

Step 5: Make connection from MongoClient to the MongoDB Server with the help of URL.

</>
Copy
MongoClient.connect(url, <callback_function>);

Once the MongoClient is done trying to make a connection, the callback function receives error and db object as arguments.

If the connection is successful, the db object points to the database, newdb.

Step 6: Get reference to MongoDB Collection.

</>
Copy
db.collection(<collection_name>, <callback_function>);

Once you get db object that points to the specified mongodb database, use it to get reference to required collection using the above statement.

Step 7: Delete MongoDB Collection.

Following is the syntax of remove() method used to delete collection in MongoDB from Node.js.

</>
Copy
collection.remove({},callback_function)

where

collectionreference to the mongodb collection that we would like to delete
callback_functionThis Node.js Callback Function is called after Node has tried deleting the specified collection, and ready with the result. The callback function receives error and result object as arguments.

Example 1 – Delete Collection in MongoDB using Node.js

In this example, we will delete a MongoDB Collection using remove() method.

node-js-mongodb-delete-collection.js

</>
Copy
// example : delete 'users' collection in newdb database
var url = "mongodb://localhost:27017/newdb";

// create a client to mongodb
var MongoClient = require('mongodb').MongoClient;

// make client connect to mongo service
MongoClient.connect(url, function(err, db) {
	if (err) throw err;
	// db pointing to newdb
	console.log("Switched to "+db.databaseName+" database");
	// get reference to collection
	db.collection("users", function(err, collection) {
		// handle the error if any
		if (err) throw err;
		// delete the mongodb collection
		collection.remove({}, function(err, result){
			// handle the error if any
			if (err) throw err;
			console.log("Collection is deleted! "+result);
			// close the connection to db when you are done with it
			db.close();
		});
	});
});

Output

~$ node node-js-mongodb-delete-collection.js 
Switched to newdb database
Collection is deleted! {"n":0,"ok":1}

Reference

MongoDB Tutorial – Learn MongoDB from basics with Examples.

Conclusion

In this Node.js MongoDB tutorial : Node.js – Delete Collection in MongoDB, we have learnt to delete a collection from MongoDB Database with Node.js Application using mongodb package. In our next tutorial – Node.js Insert Document to MongoDB Collection, we shall learn to insert one or more documents to a MongoDB collection.