次の方法で共有


$each

演算子は $each 、1 回の $addToSet 更新操作で配列フィールドに複数の要素を追加するために、or $push 演算内で使用されます。 この演算子は、複数の更新操作を実行せずに複数の項目を配列に挿入する必要がある場合に便利です。 この演算子により $each 、指定した配列内の各項目がターゲット配列に確実に追加されます。

構文

{
  $push: {
    <field>: {
      $each: [ <value1>, <value2>],
      <modifier1>: <value1>,
      <modifier2>: <value2>
    }
  }
}

パラメーター

パラメーター Description
<field> 更新するフィールド。
$each 配列フィールドに追加する値の配列。
<modifier> $sort、操作$slice$position$push動作を制御するための省略可能な修飾子。

例示

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

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

例 1: 配列に複数の要素を追加する

このクエリは、 promotionEvents 配列に複数の新しい昇格イベントを追加します。

db.stores.updateOne({
    name: "Lenore's New DJ Equipment Store"
}, {
    $push: {
        promotionEvents: {
            $each: [{
                    eventName: "Grand Savings",
                    promotionalDates: {
                        startDate: "2024-08-01",
                        endDate: "2024-08-31"
                    },
                    discounts: [{
                        categoryName: "DJ Headphones",
                        discountPercentage: 5
                    }]
                },
                {
                    eventName: "Big Bargain",
                    promotionalDates: {
                        startDate: "2024-11-25",
                        endDate: "2024-11-30"
                    },
                    discounts: [{
                        categoryName: "DJ Headphones",
                        discountPercentage: 20
                    }]
                }
            ]
        }
    }
})

このクエリは、次の結果を返します。

[
  {
    "acknowledged": true,
    "insertedId": null,
    "matchedCount": "0",
    "modifiedCount": "0",
    "upsertedCount": 0
  }
]