在 Python 中使用 PyMongo 查找和删除 MongoDB 索引
导入相应的包
from pymongo import MongoClient
from pymongo.errors import OperationFailure
建立 MongoDB 连接
client = MongoClient('mongodb://localhost:27017/')
指定 database 和 collection
db = client['test']
col = db['my_collection']
创建索引
col.create_index([('pidancode.com', pymongo.ASCENDING)], name='name_index')
查找索引
try:
index_info = col.index_information()
print(index_info)
except OperationFailure as err:
print(err)
删除索引
try:
col.drop_index('name_index')
print('索引删除成功')
except OperationFailure as err:
print(err)
相关文章