原来字段:
{ "_id" : ObjectId("5df0a28e406405edeac5001f"), "username" : "修改这一条,别的还存在不", "password" : "xxxxxxxxxxx", "open_id" : "123456789", "union_id" : "123456789", "telphone" : "123456789", "nickname" : "洛城陛下", "farm_id" : [ "牧场id1", "牧场id2" ], "is_active" : "0", "is_detele" : "0", "email" : "xxxxxx", "photo" : "https://www.123.png", "gov_addr" : "xxxxxxx", "auth_code" : "000", "register_time" : "2019-12-10", "update_time" : "2019-12-10" }
需求一:只更新 password,telephone等字段----更新一般非嵌套字段
方法:局部更新,使用 {$set:{"username":"要修改的值"}}
python代码操作
from pymongo import MongoClient # 这里需要说明,用_id当做查询条件,必须导入这个类,要不然报错 from bson.objectid import ObjectId client = MongoClient(host="127.0.0.1",port=27017) db = client["userinfo"]["user"] ret = db.update({"_id":ObjectId("5df0a28e406405edeac5001f")},{"$set":{"username":"python修改用户名"}}) print(ret) # 结果 {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
# 要更新的值,和原来的值一样,则会返回
{'n': 1, 'nModified': 0, 'ok': 1.0, 'updatedExisting': True}
修改后的字段:
{ "_id" : ObjectId("5df0a28e406405edeac5001f"), "username" : "python修改用户名", "password" : "xxxxxxxxxxx", "open_id" : "123456789", "union_id" : "123456789", "telphone" : "123456789", "nickname" : "洛城陛下", "farm_id" : [ "牧场id1", "牧场id2" ], "is_active" : "0", "is_detele" : "0", "email" : "xxxxx", "photo" : "https://www.123.png", "gov_addr" : "xxxxxxx", "auth_code" : "000", "register_time" : "2019-12-10", "update_time" : "2019-12-10" }
需求二:更新farm_id里面的第一个元素的值为 这是数组第一个元素更新后的值
方法:局部更新:数组里面的指定的元素(而非对象)的值 使用下标进行修改
数组里面放的是元素
{"name":"zhang",
"friends":["张三","李四","王五"]}
数组里面放的是字典对象
{"name":"zhang",
"friends":[{"a":1,"b":2},{"c":3,"d":4}]}
更新前的字段:就是修改后的字段
python代码操作
from pymongo import MongoClient from bson.objectid import ObjectId client = MongoClient(host="127.0.0.1",port=27017) db = client["userinfo"]["user"] ret = db.update({"_id":ObjectId("5df0a28e406405edeac5001f")},{"$set":{"farm_id.1":"python修改后的值"}}) print(ret) # 结果 {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
更新后的值
{ "_id" : ObjectId("5df0a28e406405edeac5001f"), "username" : "python修改用户名", "password" : "xxxxxxxxxxx", "open_id" : "123456789", "union_id" : "123456789", "telphone" : "123456789", "nickname" : "洛城陛下", "farm_id" : [ "牧场id1", "python修改后的值" ], "is_active" : "0", "is_detele" : "0", "email" : "xxxxxx", "photo" : "https://www.123.png", "gov_addr" : "xxxxxxx", "auth_code" : "000", "register_time" : "2019-12-10", "update_time" : "2019-12-10" }
假如数组里面放的是字典对象,怎么修改对象的key的值?
新建数据格式为:
{ "_id" : ObjectId("5df0b4bb406405edeac502a3"), "username" : "python修改用户名", "password" : "xxxxxxxxxxx", "farm_id" : [ { "a" : 1 }, { "a" : 2 } ] }
需求:修改第一个数组里面的a的值为:python修改后的值
python代码操作
from pymongo import MongoClient from bson.objectid import ObjectId client = MongoClient(host="127.0.0.1",port=27017) db = client["userinfo"]["user"] ret = db.update({"_id":ObjectId("5df0b4bb406405edeac502a3")},{"$set":{"farm_id.0.a":"python修改后的值"}}) print(ret) # 结果 {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
修改后的数据格式:
{ "_id" : ObjectId("5df0b4bb406405edeac502a3"), "username" : "python修改用户名", "password" : "xxxxxxxxxxx", "farm_id" : [ { "a" : "python修改后的值" }, { "a" : 2 } ] }
但是有一个问题,假如不知道下标,但是要精准的修改数组中,指定对象的键值对的值,可以给每个对象,增加一个唯一id字段,过滤条件换成唯一id字段,而不是_id
新建数据结构
{ "_id" : ObjectId("5df0b4bb406405edeac502a3"), "username" : "python修改用户名", "password" : "xxxxxxxxxxx", "farm_id" : [ { "a" : "python修改后的值", "id" : "1" }, { "a" : 2, "id" : "2" } ] }
python操作代码
from pymongo import MongoClient from bson.objectid import ObjectId client = MongoClient(host="127.0.0.1",port=27017) db = client["userinfo"]["user"]
# 查询条件为 farm_id.id是为了精准定位 ret = db.update({"farm_id.id":"1"},{"$set":{"farm_id.$.a":"python通过唯一字段,精准定位"}}) print(ret) # 结果 {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
修改后的字段
{ "_id" : ObjectId("5df0b4bb406405edeac502a3"), "username" : "python修改用户名", "password" : "xxxxxxxxxxx", "farm_id" : [ { "a" : "python通过唯一字段,精准定位", "id" : "1" }, { "a" : 2, "id" : "2" } ] }