Navigation

$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 $map expression has the following syntax:

{ $map: { input: <expression>, as: <string>, in: <expression> } }
Field Specification
input An expression that resolves to an array.
as Optional. A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this.
in An expression that is applied to each element of the input array. The expression references each element individually with the variable name specified in as.

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:

{ _id: 1, quizzes: [ 5, 6, 7 ] }
{ _id: 2, quizzes: [ ] }
{ _id: 3, quizzes: [ 3, 8, 9 ] }

The following aggregation operation outputs documents in which each member of the quizzes array is increased by 2.

db.grades.aggregate(
   [
      { $project:
         { adjustedGrades:
            {
              $map:
                 {
                   input: "$quizzes",
                   as: "grade",
                   in: { $add: [ "$$grade", 2 ] }
                 }
            }
         }
      }
   ]
)

This operation returns the following results:

{ "_id" : 1, "adjustedGrades" : [ 7, 8, 9 ] }
{ "_id" : 2, "adjustedGrades" : [ ] }
{ "_id" : 3, "adjustedGrades" : [ 5, 10, 11 ] }

Truncate each array element with $map

A collection named deliveries has the following documents:

{ "_id" : 1, "city" : "Bakersfield", "distances" : [ 34.57, 81.96, 44.24 ] }
{ "_id" : 2, "city" : "Barstow", "distances" : [ 73.28, 9.67, 124.36 ] }
{ "_id" : 3, "city" : "San Bernadino", "distances" : [ 16.04, 3.25, 6.82 ] }

The following aggregation operation uses the $trunc operator to truncate each member of the distances array to its integer value.

db.deliveries.aggregate(
   [
      { $project:
         {  city: "$city",
            integerValues:
               { $map:
                  {
                     input: "$distances",
                     as: "integerValue",
                     in: { $trunc: "$$integerValue" }
                  }
            }
         }
      }
   ]
)

This operation returns the following results:

{ "_id" : 1, "city" : "Bakersfield", "integerValues" : [ 34, 81, 44 ] }
{ "_id" : 2, "city" : "Barstow", "integerValues" : [ 73, 9, 124 ] }
{ "_id" : 3, "city" : "San Bernadino", "integerValues" : [ 16, 3, 6 ] }

Pipeline with two $map operations

A collection named temperatures has the following documents:

{ "_id" : 1, "date" : "June 23", "temps" : [ 4, 12, 17 ] }
{ "_id" : 2, "date" : "July 7", "temps" : [ 14, 24, 11 ] }
{ "_id" : 3, "date" : "October 30", "temps" : [ 18, 6, 8 ] }

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.

db.temperatures.aggregate(
   [
      { $project:
         {  _id: 0,
            date: "$date",
            tempsStep1:
               { $map:
                  {
                     input: "$temps",
                     as: "tempInCelsius",
                     in: { $multiply: [ "$$tempInCelsius", 9/5 ] }
                  }
               }
         }
      },
      { $project:
         {  date: "$date",
            "temps in Fahrenheit":
               { $map:
                  {
                     input: "$tempsStep1",
                     as: "tempStep1",
                     in: { $add: [ "$$tempStep1", 32 ] }
                  }
               }
         }
      }
   ]
)

This operation returns the following results:

{ "date" : "June 23", "temps in Fahrenheit" : [ 39.2, 53.6, 62.6 ] }
{ "date" : "July 7", "temps in Fahrenheit" : [ 57.2, 75.2, 51.8 ] }
{ "date" : "October 30", "temps in Fahrenheit" : [ 64.4, 42.8, 46.4 ] }

See also

$let