$setField is a MongoDB aggregation expression that returns a copy of an object with one field set to a value. The field name can be fixed or dynamic (from another field). Pair it with $getField to read that value back.
Remember
$setField = object + field = value (returns new object)
{ $setField: {
field: <name string>,
input: <object>,
value: <expression>
}
}
existing field → replaced
missing field → added
value $$REMOVE → field dropped
$input is copied — original expression not mutated
Set value to the new contents (or "$$REMOVE" to drop the field).
Piece
Required?
Role
field
Yes
Name to write (string expression)
input
Yes
Object to copy and update
value
Yes
New value (or $$REMOVE)
Pseudocode
Pseudocode
obj = evaluate(input)
name = evaluate(field) // must be a string
val = evaluate(value)
result = shallow_copy(obj)
if val is $$REMOVE:
delete result[name]
else:
result[name] = val
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while updating objects by field name.
Topic
Detail
Type
Aggregation expression (returns an object)
Syntax
{ $setField: { field, input, value } }
Missing field
Added
Existing field
Replaced
Remove
value: "$$REMOVE"
Dynamic name
field: "$key"
Sample
profile + age: 14
Try it
Live Preview
Start from { city: "Chennai" } and apply $setField-style updates.
Simulates { $setField: { field, input, value } }.
Result
input = { city: "Chennai" }
field = "age"
value = 14
→ { city: "Chennai", age: 14 }
Trace
Worked Walkthrough — Add Age to Profile
Asha’s profile only has city. We add age: 14 with $setField and keep the city.
Calculation
input = { city: "Chennai" }
field = "age"
value = 14
copy object
set age → 14
result = { city: "Chennai", age: 14 }
(original profile expression is not changed;
we assign the result back with $addFields)
Code
MongoDB Examples
We use one student with a nested profile. First insert the sample, then add age, then set a field whose name comes from another field. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine student Asha with profile: { city: "Chennai" } and a key name "score" for later. We save it in students.
Why this matters. Dot notation cannot use a runtime field name; $setField can.
Edge Cases & Pitfalls
Check these before updating objects with dynamic names.
assign
You must store the result
$setField only returns a new object. Wrap it in $addFields / $set (or an update pipeline) to keep it.
remove
$$REMOVE drops the field
Use value: "$$REMOVE" when you want the named field gone from the returned object.
name
field must be a string
The resolved field expression has to be a string. Numbers or objects as names will error.
nested
One level at a time
$setField sets a single key on one object. For deep paths, nest calls or rebuild the parent object.
Analysis
Performance Notes
Approach
Cost
Best for
$setField on small objects
Cheap object copy
Dynamic / awkward names
Fixed-name $addFields
Simpler
Known top-level fields
Huge nested objects
Copy cost grows
Keep payloads lean
Prefer plain $addFields when the field name is fixed. Reach for $setField when the name is dynamic or hard to express with dots.
Remember
Key Takeaways
Role: return an object with one field set.
Parts:field + input + value.
Dynamic:field: "$key" works at runtime.
Sample: profile + age: 14 → city kept, age added.
One line:$setField answers “give me this object with field X set to Y.”
Frequently Asked Questions
$setField returns a copy of an object with one field set to a given value. If the field exists, it is replaced; if not, it is added. The field name can be a fixed string or a dynamic expression.
Use { $setField: { field: <name>, input: <object>, value: <expression> } }. field is the name to write, input is the object to copy, and value is the new contents.
$getField reads a field from an object. $setField writes a field and returns the updated object. They pair well for dynamic field access.
Use $addFields / $set when the top-level field name is fixed. Prefer $setField when the name is dynamic, contains dots, or starts with $, or when you are updating a nested object expression.
Yes. Set value to "$$REMOVE" to drop that field from the returned object.
No. It returns a new object expression result. Persist it with $addFields / $set (or an update pipeline) if you want it written back.