如何刪除 MongoDB 資料庫中的所有內容?


可以使用 dropDatabase() 函式刪除 MongoDB 資料庫中的所有內容。語法如下 -

use yourDatabaseName;
db.dropDatabase();

以上語法將刪除 MongoDB 資料庫中的所有內容。

要刪除 MongoDB 資料庫中的所有內容,我們首先顯示來自 MongoDB 中的所有資料庫。查詢如下 -

> show dbs

以下是輸出 -

use yourDatabaseName;
admin 0.000GB
config 0.000GB
flighInformation 0.000GB
local 0.000GB
sample 0.000GB
sampleDemo 0.000GB
test 0.003GB

現在我們將刪除資料庫“flightInformation”中的所有內容。

首先,你需要將資料庫切換到“flightInformation”。查詢如下 -

> use flighInformation;
switched to db flighInformation

這裡有個查詢來刪除資料庫“flightInformation”中的所有內容 -

> db.dropDatabase();

以下是輸出 -

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

這裡有一個查詢來列出所有資料庫 -

> show dbs;

以下是輸出 -

admin 0.000GB
config 0.000GB
local 0.000GB
sample 0.000GB
sampleDemo 0.000GB
test 0.003GB

檢視示例輸出,“flightInformation”資料庫中已經刪除所有內容。

更新於:2019 年 7 月 30 日

479 次瀏覽

開啟您的 事業

完成該課程以獲得認證

開始
廣告