from pymongo import MongoClient
# 指定数据库的连接URI
uri = "mongodb://username:password@hostname:port/database_name?authSource=admin"
# 创建客户端
client = MongoClient(uri)
# 获取数据库实例
database = client["testdb"]
# 获取集合实例
collection = database["testcollection"]
# 创建集合
gold_member_conllection = database.create_collection("gold_member")
# 插入文档
document = {"name": "ff", "age": 30, "email": "ff11@ctyun.com"}
gold_member_conllection.insert_one(document)
# 删除数据
result = collection.delete_one({'name': 'ff'})
# 删除collection
database.drop_collection("test")
# 查询文档
query = {"name": "ff"}
result = collection.find_one(query)
print(result)
# 运行命令
database.command("collstats","test")
database.command("buildinfo")
# 计数
count = gold_member_conllection.find().count()
# 排序
results = gold_member_conllection.find().sort('name', pymongo.ASCENDING)
#创建索引
result=database[gold_member_conllection].create_index([('name',1)])
# 关闭连接
client.close()