$setField
The $setField operator allows adding, updating, or replacing a field in a document.
📌 Syntax
{
"$setField": {
"field": <fieldName>,
"input": <document>,
"value": <newValue>
}
}
field: The field to setinput: The input documentvalue: The new value to assign
✅ Base Example – Set Field Value
📥 Input Document
{ "a": 1 }
📌 Expression
{
"$setField": {
"field": "b",
"input": "$$ROOT",
"value": 2
}
}
📤 Output
{ "a": 1, "b": 2 }
✅ Overwrite Existing Field
📥 Input Document
{ "a": 10 }
📌 Expression
{
"$setField": {
"field": "a",
"input": "$$ROOT",
"value": 99
}
}
📤 Output
{ "a": 99 }
🧱 Ecommerce Example – Add Rating to Product Attribute
📌 Pipeline
[
{
"$project": {
"product": "$name",
"updatedAttributes": {
"$setField": {
"field": "rating",
"input": "$attributes",
"value": 4.8
}
}
}
}
]
📥 Input Document
{
"name": "Watch",
"attributes": {
"color": "Black",
"style": "Sport"
}
}
📤 Output
{
"product": "Watch",
"updatedAttributes": {
"color": "Black",
"style": "Sport",
"rating": 4.8
}
}
🔧 Common Use Cases
- Modify nested subdocuments
- Add calculated fields
- Create documents with dynamic key-value pairs
🔗 Related Operators
$getField,$mergeObjects,$unsetField,$objectToArray
🧠Notes
- Allows modifying deeply nested fields with full control
- Supports dynamic field names using expressions