共用方式為


$each

$each運算子用於 或 $addToSet 作業中$push,將多個元素新增至單一更新作業中的陣列欄位。 當您需要將多個專案插入陣列,而不需要執行多個更新作業時,這個運算子就很有用。 運算子 $each 可確保指定陣列中的每個項目都會新增至目標陣列。

語法

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

參數

參數 Description
<field> 要更新的欄位。
$each 要加入至陣列欄位的值數位。
<modifier> 選擇性修飾詞,例如 $sort$slice$position 來控制作業的行為 $push

範例

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

{
    "_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
  }
]