共用方式為


$setField

運算子 $setField 可用來新增、更新或移除內嵌檔中的欄位。 運算符允許精確作檔欄位,這讓更新巢狀欄位、重組檔,甚至完全移除欄位等工作很有用。

語法

{
  $setField: {
    field: <fieldName>,
    input: <expression>,
    value: <expression>
  }
}

參數

參數 Description
<field> 要轉換成索引鍵/值組陣列的檔 (object)。
<input> 正在處理的檔或欄位。
<value> 要指派給欄位的新值。 如果 valuenull,則會移除欄位。

範例

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

{
    "_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:更新巢狀字段

此查詢會在符合特定 _id之檔的促銷事件內巢狀折扣值上執行條件式更新。

db.stores.updateOne(
  { "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4" },
  [
    {
      $set: {
        "store.promotionEvents": {
          $map: {
            input: "$store.promotionEvents",
            as: "event",
            in: {
              $setField: {
                field: "discounts",
                input: "$$event",
                value: {
                  $map: {
                    input: "$$event.discounts",
                    as: "discount",
                    in: {
                      $cond: {
                        if: { $eq: ["$$discount.categoryName", "Laptops"] },
                        then: {
                          categoryName: "$$discount.categoryName",
                          discountPercentage: 18
                        },
                        else: "$$discount"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  ]
)

範例 2:移除欄位

此查詢會從物件中totalStaff移除staff欄位。

db.collection.updateOne(
  { "store.storeId": "12345" },
  [{
    $set: {
      "store.staff": {
        $setField: {
          field: "totalStaff",
          input: "$store.staff",
          value: null
        }
      }
    }
  }]
)