MongoDB | Delete Database using MongoShell
Prerequisite : MongoDB Introduction
Short Description : A MongoDB Database is a container for all the collections, where Collection is a bunch of MongoDB documents similar to tables in RDBMS and Document is made of fields similar to a tuple in RDBMS, but it has a dynamic schema here.
Example of a Document :
{ "Name" : "Aman", Age : 24, Gender : "Male" }
Example of a Collection :
[ { "Name" : "Aman", Age : 24, Gender : "Male" }, { "Name" : "Suraj", Age : 32, Gender : "Male" }, { "Name" : "Joyita", "Age" : 21, "Gender" : "Female" } ]
MongoShell : The mongo shell is an interactive JavaScript interface to query and update data as well as perform administrative operations in MongoDB. Detailed explanation about the Mongo shell is given on this site.
Drop a Database : In MongoDB, databases hold collections of documents. On a single MongoDB server, we can run multiple databases. when you install MongoDB some databases are automatically generated to use. many times you need to delete some database when the database is no longer used.
db.dropDatabase() the command is used to drop an existing database. This command will delete the currently selected database. If you have not selected any database, then it will delete the default ‘test’ database.
Syntax :
db.dropDatabase()
Example : The below screenshot is showing the use of “db.dropDatabase()” command for a newly created database name userDB.
How to delete a database which is not currently used : You can check currently selected database, using the command “db“. Then you can use “show dbs” command for checking the list of Databases. Then select the database you want to delete using command “use databasename“. Then execute db.dropDatabase() command to drop an existing database.
Example : In below example we were using a database name userDB and we want to delete a different database name adminDB. so first we are going to select adminDB database then we will delete this database.
References:
https://docs.mongodb.com/manual/core/databases-and-collections/#databases
Contact Us