Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB Delete Database

MongoDB Delete Database

Syntax

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

db.dropDatabase()

Delete 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 delete the database runoob.

First, view all databases:



> show dbs

admin   0.000GB

config  0.000GB

local   0.000GB

runoob  0.000GB

Next we switch to the database runoob:



> use runoob

switched to db runoob

> 

Execute the delete command:



> db.dropDatabase()

{ "dropped" : "runoob", "ok" : 1 }

Finally, we use the show dbs command again to check whether the database was deleted successfully:



> show dbs

admin   0.000GB

config  0.000GB

local   0.000GB

Delete a collection

The syntax for deleting a collection is as follows:



db.collection.drop()

The following example deletes the collection site in the runoob database:



> use runoob

switched to db runoob

> db.createCollection("runoob")     # 先创建集合,类似数据库中的表

> show tables             # show collections 命令会更加准确点

runoob

> db.runoob.drop()

true

> show tables

>