ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mongodb数据库的改操作

2019-12-11 17:55:44  阅读:340  来源: 互联网

标签:ObjectId 修改 python mongodb 数据库 farm 123456789 操作 id


原来字段:

{
    "_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"
        }
    ]
}

标签:ObjectId,修改,python,mongodb,数据库,farm,123456789,操作,id
来源: https://www.cnblogs.com/meloncodezhang/p/12024035.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有