为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

尚方宝剑之说 2022-11-18 18:18:01
我有这个文件结构{    "_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"                   }              ]         }    ]}我想更新所有具有userIdof1和handleNameof的对象John Doe。请注意,对象可以具有相同的句柄名称但不同的用户 ID。这就是为什么我需要将它与第一个匹配userId。例如我想将句柄名称更改为Donald Stark
查看完整描述

2 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

我们可以使用$[identifier]运算符来更新数组中的特定对象


所以你的更新查询应该看起来像


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' }] 

})

$[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' }] }

)

我们曾经$[]循环遍历订单数组中的所有元素,然后$[other]根据数组过滤器中的新条件更新内部数组中的特定元素


希望能帮助到你


查看完整回答
反对 回复 2022-11-18
?
湖上湖

TA贡献2003条经验 获得超2个赞

这样的事情能解决你的问题吗?


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')


查看完整回答
反对 回复 2022-11-18
  • 2 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信