Node.js – Drop Database in MongoDB

We can delete/drop a database from MongoDB through Node.js program.

In this Node.js Tutorial, we shall learn to Drop Database in MongoDB from Node.js Application with an example.

Steps to Drop Database in MongoDB via Node.js

Following is a step by step guide with an example to drop a database 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 drop (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>);

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

Step 6: Delete the Database using dropDatabase(callback) method.

</>
Copy
db.dropDatabase(<callback_function>);

Step 7: Close the connection to database.

Once all the operations are done, close the db object.

Note: In case of nested callback functions, which is the case in the below example, close the connection to database in the innermost callback function (or which gets executed last) to ensure that all the db operations are completed before closing connection.

</>
Copy
db.close();

Example 1 – Drop Database from MongoDB via Node.js

In this example, we will drop a database named newdb.

node-js-mongodb-drop-database.js

</>
Copy
// newdb is the database we drop
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;
	console.log("Connected to Database!");
	// print database name
	console.log("db object points to the database : "+ db.databaseName);
	// delete the database
	db.dropDatabase(function(err, result){
		console.log("Error : "+err);
		if (err) throw err;
		console.log("Operation Success ? "+result);
		// after all the operations with db, close it.
		db.close();
	});
});

Output

arjun@tutorialkart:~/workspace/nodejs/mongodb$ node node-js-mongodb-drop-database.js 
Connected to Database!
db object points to the database : newdb
Error : null
Operation Success ? true

Conclusion

In this Node.js MongoDB Tutorial – Node.js Drop Database in MongoDB, we have learnt to delete a database from Node.js Application using mongodb package.

In our next tutorial – Node.js Create Collection in MongoDB, we shall learn to create a MongoDB Collection.