How to drop MongoDB Database
How to drop MongoDB Database

How To Drop Database In MongoDB

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 :

  1. List out all the available databases by using the show dbs command.
  2. Select and connect to the database which is to be deleted using the use command in the MongoDB shell.
  3. Drop the connected database using the dropDatabase() function.
  4. 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.

  1. 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
  1. 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)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *