$getField
The $getField operator retrieves the value of a specified field from a document, supporting both static and dynamic keys.
📌 Syntax
{
"$getField": {
"field": <fieldName>,
"input": <document>
}
}
field: Name of the field to access (string or expression)input: The document to read from (optional — defaults to current document)
✅ Base Example – Static Field Name
📥 Input Document
{ "a": 10, "b": 20 }
📌 Expression
{
"$getField": {
"field": "a"
}
}
📤 Output
10
✅ Dynamic Field from Variable
📥 Input Document
{ "stats": { "score": 88 }, "target": "score" }
📌 Expression
{
"$getField": {
"field": "$target",
"input": "$stats"
}
}
📤 Output
88
🧱 Ecommerce Example – Access Variant Detail by Dynamic Key
📌 Pipeline
[
{
"$project": {
"variantColor": {
"$getField": {
"field": "$selectedColor",
"input": "$variants"
}
},
"product": "$name"
}
}
]
📥 Input Document
{
"name": "Backpack",
"selectedColor": "blue",
"variants": {
"red": { "stock": 5 },
"blue": { "stock": 12 }
}
}
📤 Output
{
"product": "Backpack",
"variantColor": { "stock": 12 }
}
🔧 Common Use Cases
- Dynamically access fields
- Fetch variable-key metadata
- Pair with
$mapor$objectToArrayfor dynamic access
🔗 Related Operators
$setField,$objectToArray,$getField,$mergeObjects,$literal
🧠Notes
- Avoids
$[dot]notation for dynamic fields - Returns
nullif the field does not exist