In this post, We will learn How to delete the existing mongo database.
MongoDB shell command `db.dropDatabase()` is used to drop an existing database and their associated data files. It will block other operations While dropping a database.
The basic syntax for deleting a database is
Syntax:
db.dropDatabase()
This command will drop the currently selected database. If we have not selected any database, It will drop the default database(test).
Database List:
If you want to get the list of available databases, use the below command.
> show dbsDBNAME 0.000GBadmin 0.000GBlearnmongodb 0.000GBlocal 0.000GBtest 0.000GB>
Drop Database:
First we need to select a database Which one we like to remove.
>use learnmongodbswitched to db learnmongodb>
Now If you execute `dropDatabase()` command, It will drop `learnmongodb` database.
>db.dropDatabase()
>{ "dropped" : "learnmongodb", "ok" : 1 }>
Now check your database list and your selected database has been removed.
> show dbsDBNAME 0.000GBadmin 0.000GBlocal 0.000GBtest 0.000GB>
Complete shell command:
> show dbsDBNAME 0.000GBadmin 0.000GBlearnmongodb 0.000GBlocal 0.000GBtest 0.000GB>use learnmongodbswitched to db learnmongodb>db.dropDatabase()>{ "dropped" : "learnmongodb", "ok" : 1 }> show dbsDBNAME 0.000GBadmin 0.000GBlocal 0.000GBtest 0.000GB>
Note:
If you delete a mongo database and create a new mongo database with the same name, You have to restart all mongo instances or need to `flushRouterConfig` command for all instances. We have to do this before reading or writing from the database.
0 Comments