Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新数组和内部数组中匹配的所有对象_Python_Mongodb_Mongodb Query_Pymongo_Aws Documentdb - Fatal编程技术网

Python 更新数组和内部数组中匹配的所有对象

Python 更新数组和内部数组中匹配的所有对象,python,mongodb,mongodb-query,pymongo,aws-documentdb,Python,Mongodb,Mongodb Query,Pymongo,Aws Documentdb,我有这个文档结构 { "_id": <OBJECT_ID>, "orders": [ { "userId": 1, "handleName": "John Doe", "orderAmount": 3, "others": [ { "userId": 1,

我有这个文档结构

{
    "_id": <OBJECT_ID>,
    "orders": [
         {
              "userId": 1,
              "handleName": "John Doe",
              "orderAmount": 3,
              "others": [
                   {
                        "userId": 1,
                        "handleName": "John Doe"
                   },
                   {
                        "userId": 2,
                        "handleName": "Maria Gigante"
                   }
              ]
         },
         {
              "userId": 2,
              "handleName": "Maria Gigante",
              "orderAmount": 2,
              "others": [
                   {
                        "userId": 1,
                        "handleName": "John Doe"
                   },
                   {
                        "userId": 2,
                        "handleName": "Maria Gigante"
                   }
              ]
         },
         {
              "userId": 1,
              "handleName": "John Doe",
              "orderAmount": 4,
              "others": [
                   {
                        "userId": 1,
                        "handleName": "John Doe"
                   },
                   {
                        "userId": 2,
                        "handleName": "Maria Gigante"
                   }
              ]
         },
         {
              "userId": 3,
              "handleName": "John Doe",
              "orderAmount": 4,
              "others": [
                   {
                        "userId": 1,
                        "handleName": "John Doe"
                   },
                   {
                        "userId": 2,
                        "handleName": "Maria Gigante"
                   }
              ]
         }
    ]
}
这是如何实现的

更新:
我忘了我还有另一个内部数组,我也想更新它。

这样能解决您的问题吗

d = {
    "_id": <OBJECT_ID>,
    "orders": [
         {
              "userId": 1,
              "handleName": "John Doe",
              "orderAmount": 3
         },
         {
              "userId": 2,
              "handleName": "Maria Gigante",
              "orderAmount": 2
         },
         {
              "userId": 1,
              "handleName": "John Doe",
              "orderAmount": 4
         },
         {
              "userId": 3,
              "handleName": "John Doe",
              "orderAmount": 4
         }
    ]
}

def change_name(usr_id, old_name, new_name):
    for usr in d['orders']:
        if usr['userId'] == usr_id and usr['handleName'] == old_name:
            usr['handleName'] = new_name

change_name(1, 'John Doe', 'Donald Stark')
d={
“_id”:,
“命令”:[
{
“用户ID”:1,
“handleName”:“John Doe”,
“订单金额”:3
},
{
“用户ID”:2,
“handleName”:“Maria Gigante”,
“订单金额”:2
},
{
“用户ID”:1,
“handleName”:“John Doe”,
“订单金额”:4
},
{
“用户ID”:3,
“handleName”:“John Doe”,
“订单金额”:4
}
]
}
def更改名称(usr\U id、旧名称、新名称):
对于d[“订单”]中的usr:
如果usr['userId']==usr\u id和usr['handleName']==old\u名称:
usr['handleName']=新名称
更改名称(1,‘约翰·多伊’、‘唐纳德·斯塔克’)

这样做能解决您的问题吗

d = {
    "_id": <OBJECT_ID>,
    "orders": [
         {
              "userId": 1,
              "handleName": "John Doe",
              "orderAmount": 3
         },
         {
              "userId": 2,
              "handleName": "Maria Gigante",
              "orderAmount": 2
         },
         {
              "userId": 1,
              "handleName": "John Doe",
              "orderAmount": 4
         },
         {
              "userId": 3,
              "handleName": "John Doe",
              "orderAmount": 4
         }
    ]
}

def change_name(usr_id, old_name, new_name):
    for usr in d['orders']:
        if usr['userId'] == usr_id and usr['handleName'] == old_name:
            usr['handleName'] = new_name

change_name(1, 'John Doe', 'Donald Stark')
d={
“_id”:,
“命令”:[
{
“用户ID”:1,
“handleName”:“John Doe”,
“订单金额”:3
},
{
“用户ID”:2,
“handleName”:“Maria Gigante”,
“订单金额”:2
},
{
“用户ID”:1,
“handleName”:“John Doe”,
“订单金额”:4
},
{
“用户ID”:3,
“handleName”:“John Doe”,
“订单金额”:4
}
]
}
def更改名称(usr\U id、旧名称、新名称):
对于d[“订单”]中的usr:
如果usr['userId']==usr\u id和usr['handleName']==old\u名称:
usr['handleName']=新名称
更改名称(1,‘约翰·多伊’、‘唐纳德·斯塔克’)
我们可以使用操作符更新数组中的特定对象

因此,您的更新查询应该如下所示

db.collection.updateOne(
    { _id: <OBJECT_ID> }, // filter part, add the real objectId here
    { $set: { 'orders.$[order].handleName': 'Donald Stark' } }, // update part
    { arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }] 
})
db.collection.updateOne(
{u id:},//过滤器部分,在此处添加真实的objectId
{$set:{'orders.$[order].handleName':'Donald Stark'},//更新部分
{arrayFilters:[{'order.userId':1'order.handleName':'John Doe'}]
})
$[order]仅更新与数组筛选器中的条件匹配的对象


更新 如果您有一个内部数组,并且您需要对该内部数组中的元素执行相同的操作,我们可以使用类似的方法

db.collection.updateOne(
    { _id: <OBJECT_ID> }, // filter part, add the real objectId here
    { $set: { 
        'orders.$[order].handleName': 'Donald Stark', // to set the handleName in the elements of the outer array
        'orders.$[].others.$[other].handleName': 'Donald Stark' // to set the handleName in the elements of the inner array
    } },
    { arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }, { 'other.userId': 1, 'other.handleName': 'John Doe' }] }
)
db.collection.updateOne(
{u id:},//过滤器部分,在此处添加真实的objectId
{$set:{
'orders.$[order].handleName':'Donald Stark',//在外部数组的元素中设置handleName
'orders.$[].others.$[other].handleName':'Donald Stark'//在内部数组的元素中设置handleName
} },
{ArrayFilter:[{'order.userId':1,'order.handleName':'John Doe'},{'other.userId':1,'other.handleName':'John Doe'}
)
我们使用
$[]
循环orders数组中的所有元素,然后使用
$[other]
根据数组过滤器中的新条件更新内部数组中的特定元素

希望它有帮助

我们可以使用操作符来更新数组中的特定对象

因此,您的更新查询应该如下所示

db.collection.updateOne(
    { _id: <OBJECT_ID> }, // filter part, add the real objectId here
    { $set: { 'orders.$[order].handleName': 'Donald Stark' } }, // update part
    { arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }] 
})
db.collection.updateOne(
{u id:},//过滤器部分,在此处添加真实的objectId
{$set:{'orders.$[order].handleName':'Donald Stark'},//更新部分
{arrayFilters:[{'order.userId':1'order.handleName':'John Doe'}]
})
$[order]仅更新与数组筛选器中的条件匹配的对象


更新 如果您有一个内部数组,并且您需要对该内部数组中的元素执行相同的操作,我们可以使用类似的方法

db.collection.updateOne(
    { _id: <OBJECT_ID> }, // filter part, add the real objectId here
    { $set: { 
        'orders.$[order].handleName': 'Donald Stark', // to set the handleName in the elements of the outer array
        'orders.$[].others.$[other].handleName': 'Donald Stark' // to set the handleName in the elements of the inner array
    } },
    { arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }, { 'other.userId': 1, 'other.handleName': 'John Doe' }] }
)
db.collection.updateOne(
{u id:},//过滤器部分,在此处添加真实的objectId
{$set:{
'orders.$[order].handleName':'Donald Stark',//在外部数组的元素中设置handleName
'orders.$[].others.$[other].handleName':'Donald Stark'//在内部数组的元素中设置handleName
} },
{ArrayFilter:[{'order.userId':1,'order.handleName':'John Doe'},{'other.userId':1,'other.handleName':'John Doe'}
)
我们使用
$[]
循环orders数组中的所有元素,然后使用
$[other]
根据数组过滤器中的新条件更新内部数组中的特定元素


希望有帮助

抱歉,数据在文档数据库集合中抱歉,但数据在文档数据库集合中请使用您尝试过的代码更新您的问题。这是否回答了您的问题@很抱歉,我忘了我还有一个内部数组需要更新。我已经更新了我的问题,请再次查看。请用您尝试过的代码更新您的问题。这是否回答了您的问题@很抱歉,我忘了我还有一个内部数组需要更新。我已经更新了我的问题,请再看一遍。这确实是正确的答案,但我已经更新了我的问题。我还有一个内部数组,包含类似的数据,我也希望更新。好的,我已经更新了我的答案来处理这个问题,你能检查一下吗?确实这是正确的答案,但我已经更新了我的问题。我还有一个内部数组,其中包含类似的数据,我也想更新。好的,我已经更新了我的答案来处理这个问题,你能检查一下吗?