MongoDB Drop Database

Syntax

The syntax for dropping a database in MongoDB is as follows:

db.dropDatabase()

Drop the current database, which defaults to test. You can use the db command to view the current database name.

Example

In the following example, we drop the database example.

First, view all databases:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
example  0.000GB

Next, switch to the database example:

> use example
switched to db example
> 

Execute the drop command:

> db.dropDatabase()
{ "dropped" : "example", "ok" : 1 }

Finally, use the show dbs command to check whether the database was successfully dropped:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Drop Collection

The syntax for dropping a collection is as follows:

db.collection.drop()

The following example drops the collection site in the example database:

> use example
switched to db example
> db.createCollection("example")     # 先创建集合,类似数据库中的表
> show tables             # show collections 命令会更加准确点
example
> db.example.drop()
true
> show tables
> 
Other Extensions