MongoDB tutorial : How to drop a database in MongoDB

Introduction :

Drop a database means deleting a database. If you drop a database, it will remove everything inside it.In this tutorial, we will show you how to drop a database in MongoDB.

Drop a database in MongoDB :

Command db.dropDatabase() is used to drop any database if you are currently inside the database. It takes global write lock and blocks all commands until the drop command execution is completed. One thing you should note that it will not delete all users associated with the database. If you want to delete associated users, use dropAllUsersFromDatabase() command.

Example :

1. Show all databases in the system :

show dbs

2. Create one new database Users :

use Users

3. Insert data to the databse first. Else, it will not be visible to the database list :

db.Users.insert({"name" : "Albert"})

4. Now, show all database list again and use this new database :

show dbs
use Users

5. Drop this database. Verify that it is actually dropped. :

db.dropDatabase()
show dbs

Drop a database in MongoDB

Leave a Reply