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:
- mongoosepractice (with no documents)
- userdetails (with 18 documents)
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.
1 | console.log(`Our Current Database Name : ${connection.db.databaseName}`); |
Index.js Code Snippet:
1 2 3 4 5 6 7 8 9 | 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:
1 2 3 4 5 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 | 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.
1 2 3 | 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:
1 2 3 4 5 6 | 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.
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
1 2 3 4 5 6 7 8 9 10 11 12 | 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.`) ); }); |
1 2 3 4 5 6 | 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 |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started