亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更新數組和內部數組中匹配的所有對象

更新數組和內部數組中匹配的所有對象

尚方寶劍之說 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 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號