3.10.1. Grammar ¶
The syntax format for MongoDB to delete a database is as follows:
db.dropDatabase()
Delete the current database. The default is test. You can use the
db
Command to view the current database name.
3.10.2. Example ¶
For the following example, we deleted the database runoob.
First, look at all the 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 show dbs to command whether the database has been deleted successfully:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
3.10.3. Delete a collection ¶
The collection deletion syntax is in the following format:
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
>