PyMongo:JSON密钥在mongo中更新

时间:2016-06-07 08:06:09

标签: python json mongodb pymongo

我在mongo中更新嵌套的json结构时遇到了困难。 我正在使用pymongo和Mongoengine-Rest-framework。

由于这个特殊的json具有动态结构并且嵌套很多,所以我选择使用pymongo而不是mongo-engine ORM。

创建,检索和删除操作非常好。 我想对更新问题提出一些建议。

让我们考虑一个已存在于mongo中的示例对象:

st1 = {
        "name": "Some_name",
        "details": {
                    "address1": {
                        "house_no": "731",
                        "street": "Some_street",
                        "city": "some_city"
                            "state": "some_state"
                        }
                }
        }

如果我尝试通过在update命令中发送json st2来添加地址2来更新st1,其中_id是更新的条件,

st2 = {
        "details": {
                    "address2": {
                        "house_no": "5102",
                        "street": "Some_street",
                        "city": "some_city"
                            "state": "some_state"
                        }
                }
        }

我在mongo中获得了以下对象st3,

st3 = {
        "name": "Some_name",
        "details": {
                    "address2": {
                        "house_no": " 5102",
                        "street": "Some_street",
                        "city": "some_city"
                            "state": "some_state"
                        }
                }
        }

而不是预期的st4对象。

st4 = {
        "name": "Some_name",
        "details": {
                    "address1": {
                        "house_no": "731",
                        "street": "Some_street",
                        "city": "some_city"
                            "state": "some_state"
                        },
                    "address2": {
                        "house_no": "5102",
                        "street": "Some_street",
                        "city": "some_city"
                            "state": "some_state"
                        }
                }
        }

我的更新命令是:

result = collection.update_one({'_id': id}, doc)

其中

id:文档的_id

doc :(此处)st2

collection:pymongo colllection object

原始JSON深度为6,键是动态的。不同深度需要更新。

2 个答案:

答案 0 :(得分:1)

首先,将对象更改为更新:

to_update = {
   "house_no": "5102",
   "street": "Some_street",
   "city": "some_city",
   "state": "some_state"
}

然后使用它来更新所需文档的特定部分:

collection.update_one({_id: id}, { '$set': {"details.address2" : to_update} });

答案 1 :(得分:0)

使用它来添加地址2:

  

collection.update({'_ id':ObjectId(doc_id)},{'$ set':{'details。%s'%   'address2':address2}},upsert = True)

结帐完整代码:

import pymongo
from bson.objectid import ObjectId

data = {"name": "Some_name",
       "details": {"address1": {"house_no": "731", "street": "Some_street", "city": "some_city", "state": "some_state"}}}

address2 = {"house_no": "731", "street": "Some_street", "city": "some_city", "state": "some_state"}

connect = pymongo.MongoClient('192.168.4.202', 20020)
database = connect['my_test']
collection = database['coll']

# # CREATE COLLECTIONS AND INSERT DATA
# _id = collection.insert(data)
# print _id

doc_id = '57568aa11ec52522343ee695'

collection.update({'_id': ObjectId(doc_id)}, {'$set': {'details.%s' % 'address2': address2}}, upsert=True)