MongoDB 的 dropDatabase() 命令用于刪除已有數(shù)據(jù)庫。
dropDatabase() 命令的語法格式如下:
db.dropDatabase()
它將刪除選定的數(shù)據(jù)庫。如果沒有選定要刪除的數(shù)據(jù)庫,則它會將默認的 test 數(shù)據(jù)庫刪除。
首先使用 show dbs 來列出已有的數(shù)據(jù)庫。
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
如果想刪除新數(shù)據(jù)庫 <mydb>,如下面這樣使用 dropDatabase() 方法:
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
再來看一下數(shù)據(jù)庫列表,確實刪除了 <mydb>。
>show dbs
local 0.78125GB
test 0.23012GB
>