使用 PyMongo 更新数组内的对象 [英] Updating an object inside an array with PyMongo

查看:79
本文介绍了使用 PyMongo 更新数组内的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 PyMongo/MongoDB 通过选择文档(行)然后进入嵌套数组并选择特定对象来更新嵌套数组.

I am wondering how do you update a nested array with PyMongo/MongoDB by selecting a document(row) and then going into the nested array and selecting a specific object.

{
    "_id"    : "12345",
    "name"   : "John Doe,
    "mylist" : [
         {
            "nested_id" : "1",
            "data1"     : "lorem ipsum",
            "data2"     : "stackoverflow",
            "data3"     : "james bond"
         },
         {
            "nested_id" : "2",
            "data1"     : "lorem ipsum",
            "data2"     : "stackoverflow",
            "data3"     : "james bond"
         },
         {
            ....
         }     
      ]
}

然后假设您传递了一个包含要更新的元素的自由裁量权.在这个例子中只更新data1data3

and then lets say you pass a discretionary with the elements you want to update. In this example only update data1 and data3

data = {
   "data1" : "new lorem",
   "data3" : "goldeneye"       
} 

我尝试了以下语法,但没有成功.

I have tried with the following syntax, but with no success.

db.testing.find_and_modify(
            query={"_id": "12345", 'mylist.nested_id' : "1"},
            update={"$set": {'mylist' : data}})

更新后的样子

{
        "_id"    : "12345",
        "name"   : "John Doe,
        "mylist" : [
             {
                "nested_id" : "1",
                "data1"     : "new lorem",
                "data2"     : "stackoverflow",
                "data3"     : "goldeneye"
             },
             {
                "nested_id" : "2",
                "data1"     : "lorem ipsum",
                "data2"     : "stackoverflow",
                "data3"     : "james bond"
             },
             {
                ....
             }     
          ]
    }

推荐答案

使用 点符号" 和更新部分中的位置运算符.还要转换您的输入以匹配键表示的点符号"形式:

Use "dot notation" and the positional operator in the update portion. Also transform your input to match the "dot notation" form for the key representation:

# Transform to "dot notation" on explicit field
for key in data:
    data["mylist.$." + key] = data[key]
    del data[key]

# Basically makes
# { 
#      "mylist.$.data1": "new lorem",
#      "mylist.$.data3": "goldeneye"
# }

db.testing.find_and_modify(
    query = {"_id": "12345", 'mylist.nested_id' : "1"},
    update = { "$set": data }
)

这样就会将 $ 从更新的查询部分转置到实际匹配的元素位置.匹配的数组元素将被更新,使用点表示法"只会影响提到的字段.

So that will transpose $ to the actual matched element position from the query portion of the update. The matched array element will be updated and using "dot notation" only the mentioned fields will be affected.

不知道在这种情况下服务"应该是什么意思,我只是将其视为转录错误",因为您显然是在尝试将数组元素匹配到位.

Have no idea what "service" is supposed to mean in this context and I am just treating it as a "transcribing error" since you are clearly trying to match an array element in position.

这可能更简洁,但这应该可以让您大致了解.

That could be cleaner, but this should give you the general idea.

这篇关于使用 PyMongo 更新数组内的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆