现在chat集合里有3条数据
import pymongo mongo_client = pymongo.MongoClient( host='192.168.0.112', port=27017, username="admin", password="123456" ) mongo_db = mongo_client["db1"] # 更新数据 res = mongo_db.chat.find() for i in res: print(i)
{'_id': ObjectId('5cb0ba3abd99392b1427c25e')} {'_id': ObjectId('5cb0bbf9bd993914d8b5d82c'), 'name': 'jack', 'age': 13} {'_id': ObjectId('5cb0bbf9bd993914d8b5d82d'), 'name': 'mike', 'age': 33}
更新
update_one()
传入前面条件,后面关键词
modified_count :拿到更新的条数
import pymongo mongo_client = pymongo.MongoClient( host='192.168.0.112', port=27017, username="admin", password="123456" ) mongo_db = mongo_client["db1"] # 更新数据 res = mongo_db.chat.update_one({"age":13},{"$set":{"age":34}}) # modified_count 返回更新的条数 print(res,res.modified_count) # 返回对象 # <pymongo.results.UpdateResult object at 0x0000000002EDBF08> # 1代表更新一条数据 # 1
更新成功
import pymongo mongo_client = pymongo.MongoClient( host='192.168.0.112', port=27017, username="admin", password="123456" ) mongo_db = mongo_client["db1"] # 更新数据 res = mongo_db.chat.find_one({"age":34}) print(res) # {'_id': ObjectId('5cb0bbf9bd993914d8b5d82c'), 'name': 'jack', 'age': 34}
update_many():
更新多条数据
把age>=0的数据 age都改成888
import pymongo mongo_client = pymongo.MongoClient( host='192.168.0.112', port=27017, username="admin", password="123456" ) mongo_db = mongo_client["db1"] # 更新数据 res = mongo_db.chat.update_many( {"age":{"$gte":0}},{"$set":{"age":888}} ) print(res,res.modified_count) # 返回对象 # <pymongo.results.UpdateResult object at 0x0000000002EDBF08> # 2代表更新2条数据 # 2