$map
The $map operator applies a transformation expression to each element of an input array.
📌 Syntax
{
"$map": {
"input": <arrayExpression>,
"as": <variableName>,
"in": <expression>
}
}
input: The array to iterateas: The name of the variable for each elementin: The expression to evaluate for each element
✅ Base Example – Multiply All by 2
📥 Input Document
{ "values": [1, 2, 3, 4] }
📌 Expression
{
"$map": {
"input": "$values",
"as": "num",
"in": { "$multiply": ["$$num", 2] }
}
}
📤 Output
[2, 4, 6, 8]
✅ Base Example – Uppercase Items
📥 Input Document
{ "tags": ["sale", "new", "hot"] }
📌 Expression
{
"$map": {
"input": "$tags",
"as": "tag",
"in": { "$toUpper": "$$tag" }
}
}
📤 Output
["SALE", "NEW", "HOT"]
🧱 Ecommerce Example – Compute Total per Item
📌 Pipeline
[
{
"$project": {
"orderId": 1,
"items": {
"$map": {
"input": "$items",
"as": "item",
"in": {
"name": "$$item.name",
"total": {
"$multiply": ["$$item.price", "$$item.quantity"]
}
}
}
}
}
}
]
📥 Input Document
{
"orderId": 123,
"items": [
{ "name": "Pen", "price": 2, "quantity": 10 },
{ "name": "Notebook", "price": 5, "quantity": 3 }
]
}
📤 Output
{
"orderId": 123,
"items": [
{ "name": "Pen", "total": 20 },
{ "name": "Notebook", "total": 15 }
]
}
🔧 Common Use Cases
- Transform items in an array
- Compute fields per subdocument
- Prepare output for
$projector$group
🔗 Related Operators
$filter,$reduce,$concatArrays,$arrayElemAt
🧠Notes
- Use
$$prefix when referencing variables in$map. - Input must be an array; otherwise, returns
null.