@@ -7,7 +7,7 @@ $map (aggregation)
77.. contents:: On this page
88 :local:
99 :backlinks: none
10- :depth: 1
10+ :depth: 2
1111 :class: singlecol
1212
1313Definition
@@ -56,111 +56,149 @@ Definition
5656Examples
5757--------
5858
59- Add to each element of an array using ``$map``
60- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59+ Add to Each Element of an Array
60+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6161
6262From the :binary:`~bin.mongo` shell, create a sample collection named
6363``grades`` with the following documents:
6464
6565.. code-block:: javascript
6666
67- db.grades.insertMany([
68- { _id: 1, quizzes: [ 5, 6, 7 ] },
69- { _id: 2, quizzes: [ ] },
70- { _id: 3, quizzes: [ 3, 8, 9 ] }
71- ])
67+ db.grades.insertMany( [
68+ { quizzes: [ 5, 6, 7 ] },
69+ { quizzes: [ ] },
70+ { quizzes: [ 3, 8, 9 ] }
71+ ] )
7272
7373The following aggregation operation uses :expression:`$map` with the
7474:expression:`$add` expression to increment each element in the
7575``quizzes`` array by ``2``.
7676
7777.. code-block:: javascript
7878
79- db.grades.aggregate(
80- [
81- { $project:
82- { adjustedGrades:
83- {
84- $map:
85- {
86- input: "$quizzes",
87- as: "grade",
88- in: { $add: [ "$$grade", 2 ] }
89- }
90- }
79+ db.grades.aggregate( [
80+ {
81+ $project: {
82+ adjustedGrades: {
83+ $map: {
84+ input: "$quizzes",
85+ as: "grade",
86+ in: { $add: [ "$$grade", 2 ] }
87+ }
9188 }
9289 }
93- ]
94- )
90+ }
91+ ] )
9592
9693This operation returns the following results:
9794
9895.. code-block:: javascript
9996 :copyable: false
10097
101- { "_id" : 1, "adjustedGrades" : [ 7, 8, 9 ] }
102- { "_id" : 2, "adjustedGrades" : [ ] }
103- { "_id" : 3, "adjustedGrades" : [ 5, 10, 11 ] }
104-
105- Truncate each array element with ``$map``
106- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98+ [
99+ {
100+ _id: ObjectId("6390b8f7237da390c6869a62"),
101+ adjustedGrades: [ 7, 8, 9 ]
102+ },
103+ {
104+ _id: ObjectId("6390b8f7237da390c6869a63"),
105+ adjustedGrades: []
106+ },
107+ {
108+ _id: ObjectId("6390b8f7237da390c6869a64"),
109+ adjustedGrades: [ 5, 10, 11 ]
110+ }
111+ ]
112+
113+ Truncate Each Array Element
114+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
107115
108116From the :binary:`~bin.mongo` shell, create a sample collection named
109117``deliveries`` with the following documents:
110118
111119.. code-block:: javascript
112120
113- db.deliveries.insertMany([
114- { "_id" : 1, "city" : "Bakersfield", "distances" : [ 34.57, 81.96, 44.24 ] },
115- { "_id" : 2, "city" : "Barstow", "distances" : [ 73.28, 9.67, 124.36 ] },
116- { "_id" : 3, "city" : "San Bernadino", "distances" : [ 16.04, 3.25, 6.82 ] }
117- ])
121+ db.deliveries.insertMany( [
122+ {
123+ "city" : "Bakersfield",
124+ "distances" : [ 34.57, 81.96, 44.24 ]
125+ },
126+ {
127+ "city" : "Barstow",
128+ "distances" : [ 73.28, 9.67, 124.36 ]
129+ },
130+ {
131+ "city" : "San Bernadino",
132+ "distances" : [ 16.04, 3.25, 6.82 ]
133+ }
134+ ] )
118135
119136The following aggregation operation uses :expression:`$map` to
120137:expression:`truncate <$trunc>` each element in the ``distances`` array
121138to its integer.
122139
123140.. code-block:: javascript
124141
125- db.deliveries.aggregate(
126- [
127- { $project:
128- { city: "$city",
129- integerValues:
130- { $map:
131- {
132- input: "$distances",
133- as: "decimalValue",
134- in: { $trunc: "$$decimalValue" }
135- }
142+ db.deliveries.aggregate( [
143+ {
144+ $project: {
145+ city: "$city",
146+ integerValues: {
147+ $map: {
148+ input: "$distances",
149+ as: "decimalValue",
150+ in: { $trunc: "$$decimalValue" }
136151 }
137152 }
138153 }
139- ]
140- )
154+ }
155+ ] )
141156
142157This operation returns the following results:
143158
144159.. code-block:: javascript
145160 :copyable: false
146161
147- { "_id" : 1, "city" : "Bakersfield", "integerValues" : [ 34, 81, 44 ] }
148- { "_id" : 2, "city" : "Barstow", "integerValues" : [ 73, 9, 124 ] }
149- { "_id" : 3, "city" : "San Bernadino", "integerValues" : [ 16, 3, 6 ] }
150-
151- Convert Celsius Temperatures to Fahrenheit Using ``$map``
152- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162+ [
163+ {
164+ _id: ObjectId("6390b9b1237da390c6869a65"),
165+ city: 'Bakersfield',
166+ integerValues: [ 34, 81, 44 ]
167+ },
168+ {
169+ _id: ObjectId("6390b9b1237da390c6869a66"),
170+ city: 'Barstow',
171+ integerValues: [ 73, 9, 124 ]
172+ },
173+ {
174+ _id: ObjectId("6390b9b1237da390c6869a67"),
175+ city: 'San Bernadino',
176+ integerValues: [ 16, 3, 6 ]
177+ }
178+ ]
179+
180+ Convert Celsius Temperatures to Fahrenheit
181+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153182
154183From the :binary:`~bin.mongo` shell, create a sample collection named
155184``temperatures`` with the following documents:
156185
157186.. code-block:: javascript
158187
159- db.temperatures.insertMany([
160- { "_id" : 1, "date" : ISODate("2019-06-23"), "tempsC" : [ 4, 12, 17 ] },
161- { "_id" : 2, "date" : ISODate("2019-07-07"), "tempsC" : [ 14, 24, 11 ] },
162- { "_id" : 3, "date" : ISODate("2019-10-30"), "tempsC" : [ 18, 6, 8 ] }
163- ])
188+ db.temperatures.insertMany( [
189+ {
190+ "date" : ISODate("2019-06-23"),
191+ "tempsC" : [ 4, 12, 17 ]
192+ },
193+ {
194+ "date" : ISODate("2019-07-07"),
195+ "tempsC" : [ 14, 24, 11 ]
196+ },
197+ {
198+ "date" : ISODate("2019-10-30"),
199+ "tempsC" : [ 18, 6, 8 ]
200+ }
201+ ] )
164202
165203The following aggregation operation uses the :pipeline:`$addFields`
166204stage to add a new field to the documents called ``tempsF`` which
@@ -172,17 +210,18 @@ values by ``9/5`` and then :expression:`$add` ``32``.
172210.. code-block:: javascript
173211
174212 db.temperatures.aggregate( [
175- { $addFields:
176- {
177- "tempsF":
178- { $map:
179- {
180- input : "$tempsC ",
181- as: "tempInCelsius",
182- in: { $add: [ { $multiply: [ "$$tempInCelsius", 9/5 ] }, 32 ] }
213+ {
214+ $addFields: {
215+ "tempsF": {
216+ $map: {
217+ input: "$tempsC",
218+ as : "tempInCelsius ",
219+ in: {
220+ $add: [ { $multiply: [ "$$tempInCelsius", 9/5 ] }, 32 ]
183221 }
184222 }
185- }
223+ }
224+ }
186225 }
187226 ] )
188227
@@ -191,12 +230,32 @@ This operation returns the following results:
191230.. code-block:: javascript
192231 :copyable: false
193232
194- { "_id" : 1, "date" : ISODate("2019-06-23T00:00:00Z"), "tempsC : [ 4, 12, 17 ], "tempsF" : [ 39.2, 53.6, 62.6 ] }
195- { "_id" : 2, "date" : ISODate("2019-07-07T00:00:00Z"), "tempsC" : [ 14, 24, 11 ], "tempsF" : [ 57.2, 75.2, 51.8 ] }
196- { "_id" : 3, "date" : ISODate("2019-10-30T00:00:00Z"), "tempsC" : [ 18, 6, 8 ], "tempsF" : [ 64.4, 42.8, 46.4 ] }
233+ [
234+ {
235+ _id: ObjectId("6390ba11237da390c6869a68"),
236+ date: ISODate("2019-06-23T00:00:00.000Z"),
237+ tempsC: [ 4, 12, 17 ],
238+ tempsF: [ 39.2, 53.6, 62.6 ]
239+ },
240+ {
241+ _id: ObjectId("6390ba11237da390c6869a69"),
242+ date: ISODate("2019-07-07T00:00:00.000Z"),
243+ tempsC: [ 14, 24, 11 ],
244+ tempsF: [ 57.2, 75.2, 51.8 ]
245+ },
246+ {
247+ _id: ObjectId("6390ba11237da390c6869a6a"),
248+ date: ISODate("2019-10-30T00:00:00.000Z"),
249+ tempsC: [ 18, 6, 8 ],
250+ tempsF: [ 64.4, 42.8, 46.4 ]
251+ }
252+ ]
253+
254+ Learn More
255+ ----------
197256
198- .. seealso: :
257+ To learn more about expressions used in the previous examples, see :
199258
200- - :expression:`$let `
201- - :expression:`$add `
202- - :expression:`$multiply`
259+ - :expression:`$add `
260+ - :expression:`$let `
261+ - :expression:`$multiply`
0 commit comments