$getField is a MongoDB aggregation expression that returns the value of a named field from an object. The name can be a fixed string or another field—useful when you do not know the path at write time.
Remember
$getField = object[fieldName] for aggregation
{
$getField: {
field: <string expression>,
input: <object expression>
}
}
field → name to look up (can be dynamic)
input → object to read (omit → current doc)
missing field → missing / null in output
pair with $setField to write by name
Set field to a literal ("sales") or a dynamic value ("$pick").
Use the returned value like any other expression result.
Piece
Required?
Role
field
Yes
Field name string (or expression)
input
No
Object to read; defaults to current document
Pseudocode
Pseudocode
for each document:
obj = evaluate(input) or current document
name = evaluate(field)
if obj is null or missing:
result = null
else if name is not a field on obj:
result = missing / null
else:
result = obj[name]
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while reading fields by name.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $getField: { field, input } }
Dynamic name
field: "$pick"
vs dot path
Dot path needs a fixed name
Special names
Helpful for . or $ in field names
Write sibling
$setField
Sample check
metrics.sales via name "sales" → 45
Try it
Live Preview
Watch $getField pull a metric from an object by field name.
We use one product with a metrics object. First read a fixed field name, then read a name stored in another field. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one product with metrics.views = 120, metrics.sales = 45, and a pick field set to "sales". We save it in a collection named products.
Same stored document. Still using the Notebook metrics from Example 1.
Resolve the name first.field: "$pick" becomes "sales".
Same lookup result.metrics.sales → 45. Change pick to "views" and you get 120 without rewriting the path.
Edge Cases & Pitfalls
Check these before using $getField in production pipelines.
missing
Missing fields become null-ish
If the name is not on the object, you get a missing value (often shown as null). Validate pick when needed.
dot
Not a nested path walker
field: "a.b" looks for a single key literally named a.b, not nested a then b. Nest $getField or use a real nested path for deep objects.
fixed
Prefer dot notation when fixed
If the name never changes, "$metrics.sales" is clearer. Reserve $getField for dynamic or awkward names.
write
Need to write? Use $setField
$setField is the write-side twin for setting a field by name.
Analysis
Performance Notes
Approach
Cost
Best for
Dot path
Cheapest / clearest
Known fixed fields
$getField
Cheap lookup
Dynamic names, special keys
Many dynamic keys
Still O(docs)
Metric pickers, flexible schemas
Field lookup is cheap. Filter early with $match when only some documents need a dynamic read.
Remember
Key Takeaways
Role: read an object field by name.
Dynamic:field can come from another field.
Input: object to read; omit to use the current document.
Write twin:$setField.
One line:$getField answers “what is object[name]?”—even when name is dynamic.
Frequently Asked Questions
$getField returns the value of a named field from an object. The field name can be a literal string or a dynamic expression. Use it inside aggregation stages such as $project and $addFields.
Use { $getField: { field: <string expression>, input: <object expression> } }. field is the name to look up; input is the object to read from. You can omit input to use the current document.
Use $getField when the field name is stored in another field or computed at runtime. Dot notation only works with fixed paths known when you write the query.
Yes. It is especially helpful for field names that contain dots or start with $, where normal path syntax is awkward or invalid.
If the named field does not exist on the input object, $getField returns a missing value, which usually appears as null in the output.
$setField writes or replaces a field on an object by name. Together they support dynamic object shaping.