MongoDB - 刪除資料庫



本章我們將學習如何使用 MongoDB 命令刪除資料庫。

dropDatabase() 方法

MongoDB 的 db.dropDatabase() 命令用於刪除現有資料庫。

語法

dropDatabase() 命令的基本語法如下:

db.dropDatabase()

這將刪除選定的資料庫。如果您沒有選擇任何資料庫,則它將刪除預設的“test”資料庫。

示例

首先,使用命令 show dbs 檢查可用資料庫的列表。

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

如果您想刪除名為 <mydb> 的新資料庫,則 dropDatabase() 命令如下:

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

現在檢查資料庫列表。

>show dbs
local      0.78125GB
test       0.23012GB
>
廣告