$unset

匯總管線$unset階段用來從文件中移除指定的欄位。 當您需要排除匯總查詢結果中的特定欄位時,這可能會特別有用,例如隱私權、減少承載大小,或只是清除輸出。

語法

{
    $unset: "<field1>" | ["<field1>", "<field2>", ...]
}

參數

參數 Description
field1, field2, ... 要從檔中移除的功能變數名稱。

範例

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

{
    "_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:移除單一字段

若要從檔中移除位置欄位。

db.stores.aggregate([
  {
    $unset: "store.location"
  }
])

此查詢傳回的第一個結果是:

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "store": {
      "name": "Downtown Store",
      "sales": {
        "totalSales": 15000,
        "salesByCategory": [
          {
            "category": "Electronics",
            "totalSales": 5000
          },
          {
            "category": "Clothing",
            "totalSales": 10000
          }
        ]
      }
    }
  }
]

範例 2:移除多個欄位

若要從檔中移除位置和 sales.totalSales 字段。

db.stores.aggregate([
  {
    $unset: ["store.location", "store.sales.totalSales"]
  }
])

此查詢傳回的第一個結果是:

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "store": {
      "name": "Downtown Store",
      "sales": {
        "salesByCategory": [
          {
            "category": "Electronics",
            "totalSales": 5000
          },
          {
            "category": "Clothing",
            "totalSales": 10000
          }
        ]
      }
    }
  }
]

範例 3:移除巢狀欄位

若要從檔中移除 staff.totalStaff.fullTime 和 promotionEvents.discounts 字段。

db.stores.aggregate([
  {
    $unset: ["store.staff.totalStaff.fullTime", "store.promotionEvents.discounts"]
  }
])

此查詢傳回的第一個結果是:

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "store": {
      "name": "Downtown Store",
      "staff": {
        "totalStaff": {
          "partTime": 8
        }
      },
      "promotionEvents": ["Summer Sale", "Black Friday", "Holiday Deals"]
    }
  }
]