- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Array Aggregation Operators >
- $map (aggregation)
$map (aggregation)¶
On this page
Definition¶
-
$map¶ Applies an expression to each item in an array and returns an array with the applied results.
The
$mapexpression has the following syntax:Field Specification inputAn expression that resolves to an array. asOptional. A name for the variable that represents each individual element of the inputarray. If no name is specified, the variable name defaults tothis.inAn expression that is applied to each element of the inputarray. The expression references each element individually with the variable name specified inas.For more information on expressions, see Expressions.
Examples¶
Add to each element of an array using $map¶
A collection named grades has the following documents:
The following aggregation operation outputs documents in which each
member of the quizzes array is increased by 2.
This operation returns the following results:
Truncate each array element with $map¶
A collection named deliveries has the following documents:
The following aggregation operation uses the
$trunc operator to truncate each member of the
distances array to its integer value.
This operation returns the following results:
Pipeline with two $map operations¶
A collection named temperatures has the following documents:
The following aggregation operation uses two
$project stages to output each array of temperatures
in Celsius as an array of temperatures in Fahrenheit. The first
$project stage uses the _id: 0 construct to
suppress the _id field in the output documents.
This operation returns the following results:
See also