The $setField operator returns a new object with a named field set to a value you specify. The field name can be dynamic — ideal for flexible schemas, metric maps, and fields with special characters.
01
Set Field
Add or update.
02
field + value
Dynamic names.
03
New Object
Returns copy.
04
Aggregation
$project / $addFields.
05
Use Cases
Metrics, config.
06
vs $getField
Write vs read.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $setField operator takes an object, sets one field to a new value, and returns the updated object. If the field already exists, its value is replaced. If it does not exist, the field is added. The original input object is not modified unless you assign the result back to a field.
This is the write counterpart to $getField. Use it when field names come from document data — like updating whichever metric key is stored in metricKey — or when field names contain dots or $ characters that dot notation cannot handle cleanly.
💡
Beginner Tip
$setField returns a new object. To persist the change, assign the result: metrics: { $setField: { ... } } inside $addFields or an update pipeline $set stage.
Foundation
📝 Syntax
The $setField operator takes an object with field, input, and value:
// Set a static field on nested object
{ field: "status", input: "$config", value: "active" }
// Set a dynamic field from document data
{ field: "$metricKey", input: "$metrics", value: 100 }
// Add field with special characters in name
{ field: "a.b", input: "$data", value: "ok" }
Syntax Rules
field — the name of the field to set (literal or expression).
input — the source object to copy and modify.
value — the new value for that field (any type).
Returns a new object with the field added or updated.
Returns null if input is null.
Use inside $project, $addFields, or $set (MongoDB 5.0+).
💡 $setField vs $getField vs dot notation
$getField — read a field value from an object
$setField — write a field value; returns updated object
Dot notation — fine for fixed paths like "config.status"
Dynamic names — use $setField when field: "$metricKey"
Update pipelines (array form) support aggregation expressions. $setField builds the new metrics object server-side without client-side merge logic.
Applications
🚀 Use Cases
Dynamic schemas — update whichever key a document points to (metrics, preferences, flags).
Nested config — add or change fields on embedded configuration objects.
Special field names — set fields containing dots or $ prefixes.
Update pipelines — compute new nested objects during server-side updates.
🧠 How $setField Works
1
MongoDB evaluates field, input, and value
The field name, source object, and new value are resolved from literals or expressions.
Input
2
A copy of the object is created
MongoDB clones input and sets the named field to value (add or replace).
Copy
3
The updated object is returned
Assign the result to a field in $project, $addFields, or an update $set stage.
Output
=
📝
Object with field set
All other fields from input remain unless overwritten.
Wrap Up
Conclusion
The $setField operator dynamically sets a field on an object and returns the updated copy. It is the write-side partner to $getField, and it shines when field names are data-driven or contain special characters.
Remember: assign the returned object to persist changes. Next in the series: $setIntersection.
Assign the returned object back to the field you want to update
Pair with $getField for read-modify-write on dynamic keys
Use for field names with dots or $ prefixes
Prefer dot notation when field paths are fixed and simple
Use update pipelines when persisting nested object changes
❌ Don’t
Assume input is mutated in place — it is not
Use $setField when a simple $set: { \"a.b\": v } suffices
Forget that null input returns null
Confuse literal field "user.name" with nested path user.name
Expect MongoDB versions before 5.0 to support $setField
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $setField
Use these points when setting fields on objects in pipelines.
5
Core concepts
📝01
Set Field
Add or replace.
Purpose
🔢02
field + input + value
Three args.
Syntax
🛠03
New object
Non-destructive.
Output
🔄04
vs $getField
Write vs read.
Pair
⚠05
Assign back
To persist.
Pattern
❓ Frequently Asked Questions
$setField returns a new object with a specified field set to a given value. If the field already exists, its value is replaced; if not, the field is added. The field name can be dynamic — stored in another field or computed at runtime.
The syntax is { $setField: { field: <string expression>, input: <object expression>, value: <expression> } }. The field argument is the name to set; input is the object to copy and modify; value is the new field value.
$getField reads a field value from an object. $setField writes a field value and returns the updated object. They are inverse operations for dynamic field access.
Use $setField when the field name is not fixed at pipeline design time, or when the name contains dots or starts with $. Dot notation like { $set: { "metrics.revenue": 100 } } only works for known, simple paths.
Use $setField inside aggregation expression stages such as $project, $addFields, and $set. It is also useful in update pipelines that use aggregation syntax (MongoDB 5.0+).