In this article we will discuss how to drop database in MongDB. Sometimes we have to delete the database that we have built inside. There are many reasons for deleting the database from the server, including: the database is no longer needed, saving space used, and so on.
Syntax
Basic syntax of drop database in MongoDB is :
db.dropDatabase()
This command will delete the selected database. If we have not selected any database, then it will delete default ‘test’ database.
Drop Database Steps in MongoDB :
- List out all the available databases by using the show dbs command.
- Select and connect to the database which is to be deleted using the use command in the MongoDB shell.
- Drop the connected database using the dropDatabase() function.
- Check the list of databases again now using the show command to confirm the deletion of the database.
Example
On this example we will drop customer database.
- List out all the available databases :
test> show dbs admin 132.00 KiB config 108.00 KiB customer 48.00 KiB local 72.00 KiB supplier 72.00 KiB
- Select and connect to the database which is to be deleted :
test> use customer switched to db customer
3. Drop the connected database :
customer> db.dropDatabase() { ok: 1, dropped: 'customer' }
4. Check the list of databases again :
customer> show dbs admin 132.00 KiB config 108.00 KiB local 72.00 KiB supplier 72.00 KiB
The above activities is as shown below :

Conclusion
On this tutorial we have learned how to drop database in MongoDB.
(Visited 208 times, 1 visits today)