$[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> 更新するために配列要素が満たす必要がある条件。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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" であるプロモーション イベント配列内の特定の要素を変更することで、'Desk ランプ' カテゴリの割引率を更新します。

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" }
    ]
  }
)