@@ -7,7 +7,7 @@ $map (aggregation)
7
7
.. contents:: On this page
8
8
:local:
9
9
:backlinks: none
10
- :depth: 1
10
+ :depth: 2
11
11
:class: singlecol
12
12
13
13
Definition
@@ -56,111 +56,149 @@ Definition
56
56
Examples
57
57
--------
58
58
59
- Add to each element of an array using ``$map``
60
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59
+ Add to Each Element of an Array
60
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61
61
62
62
From the :binary:`~bin.mongo` shell, create a sample collection named
63
63
``grades`` with the following documents:
64
64
65
65
.. code-block:: javascript
66
66
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
+ ] )
72
72
73
73
The following aggregation operation uses :expression:`$map` with the
74
74
:expression:`$add` expression to increment each element in the
75
75
``quizzes`` array by ``2``.
76
76
77
77
.. code-block:: javascript
78
78
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
+ }
91
88
}
92
89
}
93
- ]
94
- )
90
+ }
91
+ ] )
95
92
96
93
This operation returns the following results:
97
94
98
95
.. code-block:: javascript
99
96
:copyable: false
100
97
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
107
115
108
116
From the :binary:`~bin.mongo` shell, create a sample collection named
109
117
``deliveries`` with the following documents:
110
118
111
119
.. code-block:: javascript
112
120
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
+ ] )
118
135
119
136
The following aggregation operation uses :expression:`$map` to
120
137
:expression:`truncate <$trunc>` each element in the ``distances`` array
121
138
to its integer.
122
139
123
140
.. code-block:: javascript
124
141
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" }
136
151
}
137
152
}
138
153
}
139
- ]
140
- )
154
+ }
155
+ ] )
141
156
142
157
This operation returns the following results:
143
158
144
159
.. code-block:: javascript
145
160
:copyable: false
146
161
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153
182
154
183
From the :binary:`~bin.mongo` shell, create a sample collection named
155
184
``temperatures`` with the following documents:
156
185
157
186
.. code-block:: javascript
158
187
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
+ ] )
164
202
165
203
The following aggregation operation uses the :pipeline:`$addFields`
166
204
stage to add a new field to the documents called ``tempsF`` which
@@ -172,17 +210,18 @@ values by ``9/5`` and then :expression:`$add` ``32``.
172
210
.. code-block:: javascript
173
211
174
212
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 ]
183
221
}
184
222
}
185
- }
223
+ }
224
+ }
186
225
}
187
226
] )
188
227
@@ -191,12 +230,32 @@ This operation returns the following results:
191
230
.. code-block:: javascript
192
231
:copyable: false
193
232
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
+ ----------
197
256
198
- .. seealso: :
257
+ To learn more about expressions used in the previous examples, see :
199
258
200
- - :expression:`$let `
201
- - :expression:`$add `
202
- - :expression:`$multiply`
259
+ - :expression:`$add `
260
+ - :expression:`$let `
261
+ - :expression:`$multiply`
0 commit comments