Skip to content

$set

Adds or modifies fields in documents (alias for $addFields).


Syntax

{ "$set": { "fieldName": <expression>, ... } }

$set accepts the same payload as $addFields; both names map to the same stage handler.


✅ Basic Example

📌 Stage

{
  "$set": {
    "shippingFee": 10
  }
}

📥 Input

{
  "product": "Laptop",
  "price": 1200
}

📤 Output

{
  "product": "Laptop",
  "price": 1200,
  "shippingFee": 10
}

🧱 Deep Nested Pipeline Usage (Ecommerce)

[
  {
    "$set": {
      "totalWithTax": {
        "$add": [
          "$total",
          {
            "$multiply": [
              "$total",
              0.13
            ]
          }
        ]
      }
    }
  }
]

📥 Input Document

{
  "total": 1000
}

📤 Output Documents

{
  "total": 1000,
  "totalWithTax": 1130.0
}

➕ Supported Accumulators

None for this stage


🔧 Common Operators

$add, $multiply