Home >>MongoDB Tutorial >MongoDB Drop Database

MongoDB Drop Database

How to Drop Database using MongoDB

We'll see how to drop a database in this chapter using the MongoDB command.

The dropDatabase() Method

For dropping an existing database, the MongoDB db.dropDatabase() command is used.

Syntax

Basic syntax of dropDatabase() command is as follows −

db.dropDatabase()

This will delete the database that was selected. If you have not chosen a database, then the default 'test' database will be delete.

Example

Check the list of available databases first, by using the show dbscommand.

>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB
>

If you want to delete the new <mydb> database, the dropDatabase() command is as follows:

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

Now check list of databases.

>show dbs
local      0.78125GB
test       0.23012GB
>

No Sidebar ads