次の方法で共有


$pullAll

演算子は $pullAll 、指定した値のすべてのインスタンスを配列から削除するために使用されます。 この演算子は、1 回の操作で複数の特定の要素を削除して配列をクリーンアップする必要がある場合に便利です。

$pullどちらも$pullAll配列から要素を削除するために使用されますが、削除する要素を識別する方法が異なります。 $pull は、特定の条件に一致する配列からすべての要素を削除します。単純な値またはより複雑なクエリ (サブドキュメント フィールドの照合など) を指定できます。 一方、 $pullAll 完全一致の配列として指定された特定の値を削除しますが、条件やクエリはサポートされていません。 基本的には、 $pull さまざまな条件に基づいて条件付き削除を行えるので柔軟性が高くなりますが $pullAll 、固定された値のセットでのみ動作する方が簡単です。

構文

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

パラメーター

パラメーター Description
<field1> 指定した値が削除されるフィールド。
[ <value1>, <value2>, ... ] 指定したフィールドから削除する値の配列。

例示

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: 配列から複数の要素を削除する

"tag" 配列から "#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
  }
]