$map (aggregation)

Definition

  • $map
  • Applies an expression toeach item in an array and returns an array with the applied results.

The $map expression has the following syntax:

  1. { $map: { input: <expression>, as: <string>, in: <expression> } }

FieldSpecificationinputAn expression thatresolves to an array.asOptional. A name for the variable that represents eachindividual element of the input array. If no name isspecified, the variable name defaults to this.inAn expression that isapplied to each element of the input array. The expressionreferences each element individually with the variable namespecified in as.

For more information on expressions, seeExpressions.

Examples

Add to each element of an array using $map

A collection named grades has the following documents:

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

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

  1. db.grades.aggregate(
  2. [
  3. { $project:
  4. { adjustedGrades:
  5. {
  6. $map:
  7. {
  8. input: "$quizzes",
  9. as: "grade",
  10. in: { $add: [ "$$grade", 2 ] }
  11. }
  12. }
  13. }
  14. }
  15. ]
  16. )

This operation returns the following results:

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

Truncate each array element with $map

A collection named deliveries has the following documents:

  1. { "_id" : 1, "city" : "Bakersfield", "distances" : [ 34.57, 81.96, 44.24 ] }
  2. { "_id" : 2, "city" : "Barstow", "distances" : [ 73.28, 9.67, 124.36 ] }
  3. { "_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 thedistances array to its integer value.

  1. db.deliveries.aggregate(
  2. [
  3. { $project:
  4. { city: "$city",
  5. integerValues:
  6. { $map:
  7. {
  8. input: "$distances",
  9. as: "integerValue",
  10. in: { $trunc: "$$integerValue" }
  11. }
  12. }
  13. }
  14. }
  15. ]
  16. )

This operation returns the following results:

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

Pipeline with two $map operations

A collection named temperatures has the following documents:

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

The following aggregation operation uses two$project stages to output each array of temperaturesin Celsius as an array of temperatures in Fahrenheit. The first$project stage uses the _id: 0 construct tosuppress the _id field in the output documents.

  1. db.temperatures.aggregate(
  2. [
  3. { $project:
  4. { _id: 0,
  5. date: "$date",
  6. tempsStep1:
  7. { $map:
  8. {
  9. input: "$temps",
  10. as: "tempInCelsius",
  11. in: { $multiply: [ "$$tempInCelsius", 9/5 ] }
  12. }
  13. }
  14. }
  15. },
  16. { $project:
  17. { date: "$date",
  18. "temps in Fahrenheit":
  19. { $map:
  20. {
  21. input: "$tempsStep1",
  22. as: "tempStep1",
  23. in: { $add: [ "$$tempStep1", 32 ] }
  24. }
  25. }
  26. }
  27. }
  28. ]
  29. )

This operation returns the following results:

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

See also

$let