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

我有这个文件结构


{

    "_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


尚方宝剑之说
浏览 75回答 2
2回答

慕田峪4524236

我们可以使用$[identifier]运算符来更新数组中的特定对象所以你的更新查询应该看起来像db.collection.updateOne(&nbsp; &nbsp; { _id: <OBJECT_ID> }, // filter part, add the real objectId here&nbsp; &nbsp; { $set: { 'orders.$[order].handleName': 'Donald Stark' } }, // update part&nbsp; &nbsp; { arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }]&nbsp;})$[order] 仅更新与数组过滤器中的条件匹配的对象更新如果你有一个内部数组,并且你需要对该内部数组中的元素做同样的事情,我们可以使用类似的东西db.collection.updateOne(&nbsp; &nbsp; { _id: <OBJECT_ID> }, // filter part, add the real objectId here&nbsp; &nbsp; { $set: {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'orders.$[order].handleName': 'Donald Stark', // to set the handleName in the elements of the outer array&nbsp; &nbsp; &nbsp; &nbsp; 'orders.$[].others.$[other].handleName': 'Donald Stark' // to set the handleName in the elements of the inner array&nbsp; &nbsp; } },&nbsp; &nbsp; { arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }, { 'other.userId': 1, 'other.handleName': 'John Doe' }] })我们曾经$[]循环遍历订单数组中的所有元素,然后$[other]根据数组过滤器中的新条件更新内部数组中的特定元素希望能帮助到你

湖上湖

这样的事情能解决你的问题吗?d = {&nbsp; &nbsp; "_id": <OBJECT_ID>,&nbsp; &nbsp; "orders": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "userId": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "handleName": "John Doe",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orderAmount": 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "userId": 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "handleName": "Maria Gigante",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orderAmount": 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "userId": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "handleName": "John Doe",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orderAmount": 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "userId": 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "handleName": "John Doe",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orderAmount": 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; ]}def change_name(usr_id, old_name, new_name):&nbsp; &nbsp; for usr in d['orders']:&nbsp; &nbsp; &nbsp; &nbsp; if usr['userId'] == usr_id and usr['handleName'] == old_name:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usr['handleName'] = new_namechange_name(1, 'John Doe', 'Donald Stark')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python