$each

$each 运算符在 $addToSet$push 操作中使用,以便在单个更新操作中向数组字段添加多个元素。 当需要在数组中插入多个项而无需执行多个更新操作时,此运算符非常有用。 $each 运算符可确保将指定数组中的每个项添加至目标数组。

语法

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

parameters

参数 说明
<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
  }
]