MongoDB - Drop Database
In this chapter, we will see how to drop a database using MongoDB command.
The dropDatabase() Method
MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax
Basic syntax of dropDatabase() command is as follows −
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.
Example - Checking and Deleting a Database
First, check the list of available databases by using the command, show dbs.
test>show dbs admin 40.00 KiB config 108.00 KiB local 40.00 KiB mydb 40.00 KiB >
If you want to delete new database <mydb>, then dropDatabase() command would be as follows −
test>use mydb
switched to db mydb
mydb>db.dropDatabase()
{ ok: 1, dropped: 'mydb' }
>
Now check list of databases.
mydb> use test switched to db test test> show dbs admin 40.00 KiB config 108.00 KiB local 40.00 KiB test>
Advertisements