import pymongo # 创建链接 myclient = pymongo.MongoClient("mongodb://localhost:27017/") # 遍历所有库 dblist = myclient.list_database_names() print(dblist) # 创建数据库 mydb = myclient["runoobdb"] # 便利所有集合 collist = mydb. list_collection_names() # 创建集合 mycol = mydb["sites"] def insert(collect): mydict = { "name": "RUNOOB", "alexa": "10000", "url": "https://www.runoob.com" } x = collect.insert_one(mydict) def find(collect,type:int): # 1表示查询一条,2表示查询所有。3表示查询指定字段 if type == 1: x = collect.find_one() if type == 2: for i in collect.find(): print(i)
原文:https://www.cnblogs.com/yukizzc/p/13670208.html