# 选择集合
col = client['db_name']['collection_name']# 增加一条数据
ret = collection.insert_one({"name":"test10010","age":33})print(ret)# 增加多条数据
item_list =[{"name":"test1000{}".format(i)}for i inrange(10)]# insert_many接收一个列表,列表中为所有需要插入的字典
t = collection.insert_many(item_list)# 查找一条数据
t = collection.find_one({"name":"test10005"})#find_one查找并且返回一个结果,接收一个字典形式的条件print(t)# 查找条件数据# 结果是一个Cursor游标对象,是一个可迭代对象,可以类似读文件的指针,但是只能够进行一次读取#find返回所有满足条件的结果,如果条件为空,则返回数据库的所有
t = collection.find({"name":"test10005"})#结果是一个Cursor游标对象,是一个可迭代对象,可以类似读文件的指针,for i in t:print(i)for i in t:#此时t中没有内容print(i)# 更新一条数据 注意使用$set命令
collection.update_one({"name":"test10005"},{"$set":{"name":"new_test10005"}})# 更新多条数据
collection.update_many({"name":"test10005"},{"$set":{"name":"new_test10005"}})# 删除一条数据
collection.delete_one({"name":"test10010"})# 删除多条数据
collection.delete_many({"name":"test10010"})