$[identifier]

$[identifier] 陣列更新運算符是用來更新陣列中符合指定條件的特定元素。 當您需要根據特定準則更新數位內的多個元素時,這個運算符很有用。 它允許在文件中進行更細微的更新,使其成為管理複雜數據結構的強大工具。

語法

{
  <update operator>: {
    <array field>.$[<identifier>]: <value>
  }
},
{
  arrayFilters: [
    { <identifier>.<field>: <condition> }
  ]
}

參數

參數 Description
<update operator> 要套用的更新運算子(例如、 $set$inc等)。
<array field> 包含要更新之陣列的欄位。
<identifier> 中 arrayFilters 用來比對陣列中特定元素的佔位元。
<value> 要設定或更新的值。
arrayFilters 篩選條件陣列,用來識別要更新的專案。
<field> 要檢查之陣列元素內的特定欄位。
<condition> 陣列元素必須符合才能更新的條件。

範例

請參考商店集合中的此範例檔。

{
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
    "name": "Trey Research | Home Office Depot - Lake Freeda",
    "location": {
        "lat": -48.9752,
        "lon": -141.6816
    },
    "staff": {
        "employeeCount": {
            "fullTime": 12,
            "partTime": 19
        }
    },
    "sales": {
        "salesByCategory": [
            {
                "categoryName": "Desk Lamps",
                "totalSales": 37978
            }
        ],
        "revenue": 37978
    },
    "promotionEvents": [
        {
            "eventName": "Crazy Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2023,
                    "Month": 9,
                    "Day": 27
                },
                "endDate": {
                    "Year": 2023,
                    "Month": 10,
                    "Day": 4
                }
            },
            "discounts": [
                {
                    "categoryName": "Desks",
                    "discountPercentage": 25
                },
                {
                    "categoryName": "Filing Cabinets",
                    "discountPercentage": 23
                }
            ]
        },
        {
            "eventName": "Incredible Markdown Mania",
            "promotionalDates": {
                "startDate": {
                    "Year": 2023,
                    "Month": 12,
                    "Day": 26
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 1,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Monitor Stands",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Desks",
                    "discountPercentage": 24
                }
            ]
        },
        {
            "eventName": "Major Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 3,
                    "Day": 25
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 4,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Office Accessories",
                    "discountPercentage": 9
                },
                {
                    "categoryName": "Desks",
                    "discountPercentage": 13
                }
            ]
        },
        {
            "eventName": "Blowout Bonanza",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Office Chairs",
                    "discountPercentage": 24
                },
                {
                    "categoryName": "Desk Lamps",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Super Saver Fiesta",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 10,
                    "Day": 1
                }
            },
            "discounts": [
                {
                    "categoryName": "Desks",
                    "discountPercentage": 5
                },
                {
                    "categoryName": "Monitor Stands",
                    "discountPercentage": 10
                }
            ]
        }
    ],
    "company": "Trey Research",
    "city": "Lake Freeda",
    "storeOpeningDate": "2024-12-30T22:55:25.779Z",
    "lastUpdated": {
        "t": 1729983325,
        "i": 1
    }
}

範例1:更新指定促銷活動中所選類別的折扣百分比。

此查詢會修改促銷活動陣列中事件名稱為「Blowout Bonanza」的特定元素,以更新「檯燈」類別的折扣百分比。

db.stores.updateOne(
  {
    _id: "905d1939-e03a-413e-a9c4-221f74055aac",
    "promotionEvents.eventName": "Blowout Bonanza"
  },
  {
    $set: {
      "promotionEvents.$[event].discounts.$[discount].discountPercentage": 18
    }
  },
  {
    arrayFilters: [
      { "event.eventName": "Blowout Bonanza" },
      { "discount.categoryName": "Desk Lamps" }
    ]
  }
)