- Python 数据访问教程
- Python 数据访问 - 主页
- Python MySQL
- Python MySQL - 简介
- Python MySQL - 数据库连接
- Python MySQL - 创建数据库
- Python MySQL - 创建表
- Python MySQL - 插入数据
- Python MySQL - 选择数据
- Python MySQL -Where 子句
- Python MySQL - 排序依据
- Python MySQL - 更新表
- Python MySQL - 删除数据
- Python MySQL - 删除表
- Python MySQL - 限制
- Python MySQL - 加入
- Python MySQL - 游标对象
- Python PostgreSQL
- Python PostgreSQL - 简介
- Python PostgreSQL - 数据库连接
- Python PostgreSQL - 创建数据库
- Python PostgreSQL - 创建表
- Python PostgreSQL - 插入数据
- Python PostgreSQL - 选择数据
- Python PostgreSQL -Where 子句
- Python PostgreSQL - 排序依据
- Python PostgreSQL - 更新表
- Python PostgreSQL - 删除数据
- Python PostgreSQL - 删除表
- Python PostgreSQL - 限制
- Python PostgreSQL - 加入
- Python PostgreSQL - 游标对象
- Python SQLite
- Python SQLite - 简介
- Python SQLite - 建立连接
- Python SQLite - 创建表
- Python SQLite - 插入数据
- Python SQLite - 选择数据
- Python SQLite -Where 子句
- Python SQLite - 排序依据
- Python SQLite - 更新表
- Python SQLite - 删除数据
- Python SQLite - 删除表
- Python SQLite - 限制
- Python SQLite - 加入
- Python SQLite - 游标对象
- 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 数据访问资源
- Python 数据访问 - 快速指南
- Python 数据访问 - 有用的资源
- Python 数据访问 - 讨论
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