How to drop a database in MongoDB

In this MongoDB tutorial, we are going to learn “How to drop a database in MongoDB”. We will also cover this with different operations and examples. These are the following topics that we are going to cover in this tutorial:

  • How to drop a database in MongoDB
  • How to drop database in MongoDB compass
  • How to drop local database in MongoDB
  • How to drop a particular database in MongoDB
  • How to drop all databases in MongoDB

How to drop a database in MongoDB

In MongoDB, to drop a database you have to use the dropDatabase() method.

Syntax:

db.dropDatabase()

This command will drop the selected database. if you don’t select any databases then it will drop the default test database.

Note that deleting a database should be done with caution because it will delete all of the data contained within the database, including collections and documents.

You will more understand with the help of the example so let’s get into it.

Example:

Check available database in MongoDB using the command show dbs

> show dbs
local         0.526GB
mydb          0.328GB
test          0.216GB

Now, we drop the mydb database so we select that by using the use command then drop the database by using dropDatabase()

>use mydb
switched to db mydb
> db.dropDatabase()
{ "ok" : 1 }

Now, check again all the databases in the MongoDB database by using the command show dbs

> show dbs
local         0.526GB
test          0.216GB

Here, you can see the mydb database is successfully dropped from the MongoDB databases. This way you can easily drop or delete any database in MongoDB.

Read: Export MongoDB to CSV

How to drop database in MongoDB compass

The MongoDB compass provides CRUD capabilities (Create, Read, Update, Delete). you can easily drop or delete the entire collection or database in compass. You will more understand with the help of examples so let’s get into it.

Example:

For dropping the database in MongoDB compass, you need to follow the below steps:

  • Open MongoDB compass
  • Select the database that you want the drop
How to drop database in MongoDB compass
Drop database in MongoDB compass

Here, you can see we selected the database MyData and there is also a collection inside the database. To drop the MyData database, there is a button on the right side of the database called Drop database.

  • Click on Drop database button.
Drop database in MongoDB compass
Drop database in MongoDB compass

Here, For confirmation write the database MyData name again and click on the DROP DATABASE button.

  • This will successfully drop the MyData database

This way you can easily drop any database using MongoDB Compass.

Always remember that when you are dropping the database this will drop all the collections that are inside the database. Before deleting any database cross-check that you are selected the right database so that you can save your important data from being deleted.

Read: MongoDB join two collections

How to drop local database in MongoDB

The MongoDB database is a container for all the collections. Many times we need to drop the database when the database is no longer used.

To drop the database, we used the command db.dropDatabase(). This command will help you to delete the currently selected database.

Example:

In this example, you will understand how you can drop the local database in MongoDB

> show dbs
admin         0.011GB
config        0.012GB
local         0.010GB
Student       0.001GB

Here, these are some databases inside the MongoDB database. Now, we are going to drop the Student database.

Drop local database in MongoDB

Here, we select the Student database by using the use command. The use command will help you to select the database that you want to use.

After that, we successfully dropped the database by using the db.dropDatabase() command.

Read: Import CSV into MongoDB

How to drop a particular database in MongoDB

In MongoDB to drop a particular database you need to follow the below steps:

  • Select the database that you want to drop
> use Company
switched to db mydb

Here, using the “use Company” use is used to select a particular database that we want to drop and we selected the Company database.

  • Now, use the db.dropDatabase() command to drop the database
> db.dropDatabase()
{ "ok" : 1 }

This is the simplest way to drop a particular database.

Note that, if you don’t select any database then it will drop by default selected test database.

Read: MongoDB group by multiple fields

How to drop all databases in MongoDB

Sometimes, we simply want to delete all the databases because we want to start from scratch. So the following javascript code will help you to drop all the databases:

var dbs = db.getMongo().getDBNames()
for(var i in dbs){
    db = db.getMongo().getDB( dbs[i] );
    if (db.getName() !== 'admin' && db.getName() !== 'local') 
    {
        print( "dropping db " + db.getName() );  
        db.dropDatabase();
    }
}

Here, the script is quite simple. We use the getDBNames() function to take a list of all the database names.

After that, we want to drop all the databases so we check the name by using the conditional statement and if the database name is different from the local and admin then simply drop it.

Note that, we safe the local and admin database because If we delete all databases or delete the data directory then we lose users of information or any other information that is stored in the admin and local database.

You may also like reading the following MongoDB tutorials.

Conclusion

In this MongoDB tutorial, we have covered How to drop databases in MongoDB. We understood this with different operations and examples. And, we have also discussed the following topics.

  • How to drop a database in MongoDB
  • How to drop database in MongoDB compass
  • How to drop local database in MongoDB
  • How to drop a particular database in MongoDB
  • How to drop all databases in MongoDB