Drop Database Using Mongoose

Introduction

In this tutorial, we drop will learn how to drop a database using the Mongoose library for MongoDB. The Mongoose library is not the native NodeJS library for MongoDB but is a third party library which allows you to create and store data using data models.

Deleting an entire database programmatically is not a use case that happens often so make sure that you want to delete a database and not just a collection.

We will be running the script we create to delete the database using NodeJS.

Drop a database using Mongoose

In this tutorial, I’m going to use the database which we have used in previous tutorials “mongoosepractice”. 

The database contains 2 collections:

  1. mongoosepractice (with no documents)
  2. userdetails (with 18 documents)

Image from Gyazo

So, let’s get the database name first from which we are connected then we will delete that database. 

Replace the code provided below into your index.js file. On the second line we create a connection and then if the connection is successful we are displaying the connected database name.

console.log(`Our Current Database Name : ${connection.db.databaseName}`);

Index.js Code Snippet:

const mongodbconnection = require("mongoose");
mongodbconnection.connect("mongodb://127.0.0.1:27017/mongoosepractice", {
useNewUrlParser: true
});
const connection = mongodbconnection.connection;
connection.once("open", function() {
console.log("*** MongoDB got connected ***");
console.log(`Our Current Database Name : ${connection.db.databaseName}`);
});

Index.js Code Snippet Result:

C:\mongooseproject>npm start
> mongooseproject@1.0.0 start C:\mongooseproject
> node index.js
*** MongoDB got connected ***
Our Current Database Name : mongoosepractice

Now, we have all the details like database name, how many collections inside database and number of documents inside the collections in that database.

There a function called dropDatabase() which we are going to use for this tutorial. So now you update the code in index.js file and replace the code with the code snippet provided below:

const mongodbconnection = require("mongoose");
mongodbconnection.connect("mongodb://127.0.0.1:27017/mongoosepractice", {
useNewUrlParser: true
});
const connection = mongodbconnection.connection;
connection.once("open", function() {
console.log("*** MongoDB got connected ***");
console.log(`Our Current Database Name : ${connection.db.databaseName}`);
mongodbconnection.connection.db.dropDatabase(
console.log(`${connection.db.databaseName} database dropped.`)
);
});

We have used dropDatabase to delete our current database we are inside.

mongodbconnection.connection.db.dropDatabase(
console.log(`${connection.db.databaseName} database dropped.`)
);

We are dropping the MongoDB database and also printing a message in the console that this database is deleted.

Result:

C:\mongooseproject>npm start
> mongooseproject@1.0.0 start C:\mongooseproject
> node index.js
*** MongoDB got connected ***
Our Current Database Name : mongoosepractice
mongoosepractice database dropped

Let’s verify that our database is present inside the MongoDB or not. Here you can compare the screenshot taken before and now. The database “mongoosepractice” is missing. 

Image from Gyazo

Conclusion

We have achieved our goal and dropped the database successfully using mongoose. We hope you can apply this to your specific situation.

Just The Code

const mongodbconnection = require("mongoose");
mongodbconnection.connect("mongodb://127.0.0.1:27017/mongoosepractice", {
useNewUrlParser: true
});
const connection = mongodbconnection.connection;
connection.once("open", function() {
console.log("*** MongoDB got connected ***");
console.log(`Our Current Database Name : ${connection.db.databaseName}`);
mongodbconnection.connection.db.dropDatabase(
console.log(`${connection.db.databaseName} database dropped.`)
);
});
C:\mongooseproject>npm start
> mongooseproject@1.0.0 start C:\mongooseproject
> node index.js
*** MongoDB got connected ***
Our Current Database Name : mongoosepractice
mongoosepractice database dropped

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.