
- Python MongoDB 教程
- Python MongoDB - 主頁
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - 限制
- Python MongoDB 實用資源
- Python MongoDB - 快速指南
- Python MongoDB - 實用資源
- Python MongoDB - 討論
Python MongoDB - 刪除集合
你可以使用 MongoDB 的drop()方法來刪除集合。
語法
以下是 drop() 方法的語法 −
db.COLLECTION_NAME.drop()
示例
以下示例刪除名為 sample 的集合 −
> show collections myColl sample > db.sample.drop() true > show collections myColl
使用 Python 刪除集合
你可以透過呼叫 drop() 方法從當前狀態刪除/刪除一個集合。
示例
from pymongo import MongoClient #Creating a pymongo client client = MongoClient('localhost', 27017) #Getting the database instance db = client['example2'] #Creating a collection col1 = db['collection'] col1.insert_one({"name": "Ram", "age": "26", "city": "Hyderabad"}) col2 = db['coll'] col2.insert_one({"name": "Rahim", "age": "27", "city": "Bangalore"}) col3 = db['myColl'] col3.insert_one({"name": "Robert", "age": "28", "city": "Mumbai"}) col4 = db['data'] col4.insert_one({"name": "Romeo", "age": "25", "city": "Pune"}) #List of collections print("List of collections:") collections = db.list_collection_names() for coll in collections: print(coll) #Dropping a collection col1.drop() col4.drop() print("List of collections after dropping two of them: ") #List of collections collections = db.list_collection_names() for coll in collections: print(coll)
輸出
List of collections: coll data collection myColl List of collections after dropping two of them: coll myColl
廣告