MongoDB — 刪除集合



在本章中,我們將看到如何使用 MongoDB 來刪除集合。

drop() 方法

MongoDB 的 db.collection.drop() 用於從資料庫中刪除集合。

語法

drop() 命令的基本語法如下 −

db.COLLECTION_NAME.drop()

示例

首先,檢查資料庫 mydb 中的可用集合。

>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>

現在刪除名為 mycollection 的集合。

>db.mycollection.drop()
true
>

再次檢查資料庫中的集合列表。

>show collections
mycol
system.indexes
tutorialspoint
>

如果選定的集合被成功刪除,則 drop() 方法將返回true,否則將返回false。

廣告