共用方式為


$pullAll

運算子 $pullAll 是用來從陣列中移除指定值的所有實例。 當您需要藉由移除單一作業中的多個特定元素來清除數位時,這個運算元很有用。

$pull$pullAll 都用來從陣列中移除元素,但兩者在識別要移除的專案的方式上有所不同。 $pull 從符合特定條件的陣列中移除所有元素,可以是簡單值或更複雜的查詢(例如比對子檔欄位)。 另一方面, $pullAll 會移除提供做為完全相符項目陣列的特定值,但不支援條件或查詢。 基本上,它更靈活, $pull 因為它允許根據各種準則的條件式移除,雖然 $pullAll 更簡單,只能使用一組固定的值。

語法

{
  $pullAll: { <field1>: [ <value1>, <value2>] }
}

參數

參數 Description
<field1> 將移除指定值的欄位。
[ <value1>, <value2>, ... ] 要從指定欄位移除的值陣列。

範例

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

{
    "_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:從陣列中移除多個元素

若要從「標籤」陣列中移除「#MembershipDeals」和「#SeasonalSale」的折扣,請在標籤欄位上使用$pulAll運算子執行查詢,其中包含要移除的值。

db.stores.updateMany(
    //filter
    { _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"},
    {
      $pullAll: {
        tag: ["#MembershipDeals","#SeasonalSale" ]
      }
    }
)

此查詢會傳回下列結果。

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