@@ -20,24 +20,18 @@ The pipeline constructed by |charts| consists of the following segments in the f
20
20
21
21
1. `Data Source Pipeline <https://docs.mongodb.com/charts/saas/data-source-pipeline/>`_
22
22
#. `Dashboard Filters <https://docs.mongodb.com/charts/saas/dashboard-filtering/#filter-dashboards-by-field-values>`_
23
- #. `Query bar <https://docs.mongodb.com/charts/saas/filter-documents/# query-bar>`_
23
+ #. `Query Bar <https://docs.mongodb.com/charts/saas/filter-chart-results/#filter-your-data-using-the- query-bar>`_
24
24
#. `Embedding Filters <https://docs.mongodb.com/charts/saas/embedded-chart-options/#embedded-chart-options>`_
25
25
#. `Calculated Fields <https://docs.mongodb.com/charts/saas/calculated-fields/>`_
26
- #. `Chart Filter Pane <https://docs.mongodb.com/charts/saas/filter-documents/#filter-tab >`_
26
+ #. `Chart Filter Pane <https://docs.mongodb.com/charts/saas/filter-chart-results/#create-filters-for-your-data >`_
27
27
#. `Encoding <https://docs.mongodb.com/charts/saas/encoding-channels/>`_
28
+ #. `Sorting & Limiting <https://docs.mongodb.com/charts/saas/bin-data/#sort-data>`_
28
29
#. Maximum Document limit
29
30
30
31
.. note::
31
32
You do not need to configure all of the above settings when creating
32
33
a chart. Unspecified settings are skipped when generating the
33
- aggregation pipeline.
34
-
35
-
36
- .. note::
37
- Sorts and limits specified in the encoding panel are currently not
38
- included in the aggregation pipeline. Instead, the sorting and
39
- limiting is applied on the client-side while rendering the chart.
40
-
34
+ aggregation pipeline.
41
35
42
36
Example
43
37
-------
@@ -63,10 +57,11 @@ queries, calculated fields, and filters added in the :guilabel:`Filter`
63
57
pane, |charts| generates the following aggregation pipeline:
64
58
65
59
.. code-block:: javascript
66
- :emphasize-lines: 1-31
60
+ :emphasize-lines: 1-31, 33-51
61
+ :linenos:
67
62
68
63
{
69
- "$addFields": { // Encoding
64
+ "$addFields": { // Encoding
70
65
"__alias_0": {
71
66
"$sum": "$items.price"
72
67
}
@@ -91,19 +86,38 @@ pane, |charts| generates the following aggregation pipeline:
91
86
},
92
87
{
93
88
"$project": {
94
- "y": "$__alias_0",
95
89
"x": "$__alias_1",
90
+ "y": "$__alias_0",
96
91
"_id": 0
97
92
}
98
93
},
94
+
95
+ {
96
+ "$addFields": { // Sorting
97
+ "__agg_sum": {
98
+ "$sum": [
99
+ "$y"
100
+ ]
101
+ }
102
+ }
103
+ },
104
+ {
105
+ "$sort": {
106
+ "__agg_sum": -1
107
+ }
108
+ },
109
+ {
110
+ "$project": {
111
+ "__agg_sum": 0
112
+ }
113
+ },
99
114
{
100
115
"$limit": 5000
101
116
}
102
-
103
117
104
- The pipeline at this stage only consists of groups from the
105
- :guilabel:`Encode` panel and the maximum document limit, which is set to
106
- 5000 by |charts|.
118
+ The pipeline at this point consists of groups from the
119
+ :guilabel:`Encode` panel, stages for the default sort order, and the
120
+ maximum document limit, which is set to 5000 by |charts|.
107
121
108
122
Adding Queries
109
123
~~~~~~~~~~~~~~
@@ -140,9 +154,10 @@ Aggregation Pipeline:
140
154
141
155
.. code-block:: javascript
142
156
:emphasize-lines: 1-18
157
+ :linenos:
143
158
144
159
{
145
- "$match": { // Query
160
+ "$match": { // Query
146
161
"$and": [
147
162
{
148
163
"saleDate": {
@@ -164,15 +179,16 @@ Aggregation Pipeline:
164
179
"__alias_0": {
165
180
"$sum": "$items.price"
166
181
}
167
- },
182
+ }
183
+ },
168
184
{
169
185
"$group": {
170
186
"_id": {
171
187
"__alias_1": "$purchaseMethod"
172
188
},
173
189
"__alias_0": {
174
190
"$sum": "$__alias_0"
175
- }
191
+ }
176
192
}
177
193
},
178
194
{
@@ -184,16 +200,34 @@ Aggregation Pipeline:
184
200
},
185
201
{
186
202
"$project": {
187
- "y": "$__alias_0",
188
203
"x": "$__alias_1",
204
+ "y": "$__alias_0",
189
205
"_id": 0
190
206
}
191
207
},
208
+ {
209
+ "$addFields": {
210
+ "__agg_sum": {
211
+ "$sum": [
212
+ "$y"
213
+ ]
214
+ }
215
+ }
216
+ },
217
+ {
218
+ "$sort": {
219
+ "__agg_sum": -1
220
+ }
221
+ },
222
+ {
223
+ "$project": {
224
+ "__agg_sum": 0
225
+ }
226
+ },
192
227
{
193
228
"$limit": 5000
194
229
}
195
230
196
-
197
231
The aggregation pipeline now starts with the query applied, and is
198
232
followed by the groups selected in the :guilabel:`Encode` panel and the
199
233
max document limit.
@@ -221,6 +255,7 @@ Aggregation Pipeline:
221
255
222
256
.. code-block:: javascript
223
257
:emphasize-lines: 19-39
258
+ :linenos:
224
259
225
260
{
226
261
"$match": {
@@ -241,7 +276,7 @@ Aggregation Pipeline:
241
276
}
242
277
},
243
278
{
244
- "$addFields": { // Calculated Field
279
+ "$addFields": { // Calculated Field
245
280
"revenue": {
246
281
"$reduce": {
247
282
"input": "$items",
@@ -285,16 +320,34 @@ Aggregation Pipeline:
285
320
"_id": 0
286
321
}
287
322
},
323
+ {
324
+ "$addFields": {
325
+ "__agg_sum": {
326
+ "$sum": [
327
+ "$y"
328
+ ]
329
+ }
330
+ }
331
+ },
332
+ {
333
+ "$sort": {
334
+ "__agg_sum": -1
335
+ }
336
+ },
337
+ {
338
+ "$project": {
339
+ "__agg_sum": 0
340
+ }
341
+ },
288
342
{
289
343
"$limit": 5000
290
344
}
291
-
345
+
292
346
293
347
The updated pipeline now includes the calculated field right below the
294
348
query applied in the :guilabel:`Query` bar while the order of the rest
295
349
of the components remains unchanged.
296
350
297
-
298
351
Adding Filters
299
352
~~~~~~~~~~~~~~
300
353
@@ -311,7 +364,8 @@ aggregation pipeline:
311
364
Aggregation Pipeline:
312
365
313
366
.. code-block:: javascript
314
- :emphasize-lines: 40-54
367
+ :emphasize-lines: 40-48
368
+ :linenos:
315
369
316
370
{
317
371
"$match": {
@@ -353,16 +407,10 @@ Aggregation Pipeline:
353
407
}
354
408
},
355
409
{
356
- "$match": { // Filter
410
+ "$match": { // Filter
357
411
"storeLocation": {
358
- "$nin": [
359
- null,
360
- "",
361
- "Austin",
362
- "Denver",
363
- "London",
364
- "San Diego",
365
- "Seattle"
412
+ "$in": [
413
+ "New York"
366
414
]
367
415
}
368
416
}
@@ -373,20 +421,7 @@ Aggregation Pipeline:
373
421
"__alias_0": "$purchaseMethod"
374
422
},
375
423
"__alias_1": {
376
- "$sum": {
377
- "$cond": [
378
- {
379
- "$ne": [
380
- {
381
- "$type": "$Revenue "
382
- },
383
- "missing"
384
- ]
385
- },
386
- 1,
387
- 0
388
- ]
389
- }
424
+ "$sum": "$revenue"
390
425
}
391
426
}
392
427
},
@@ -404,6 +439,25 @@ Aggregation Pipeline:
404
439
"_id": 0
405
440
}
406
441
},
442
+ {
443
+ "$addFields": {
444
+ "__agg_sum": {
445
+ "$sum": [
446
+ "$y"
447
+ ]
448
+ }
449
+ }
450
+ },
451
+ {
452
+ "$sort": {
453
+ "__agg_sum": -1
454
+ }
455
+ },
456
+ {
457
+ "$project": {
458
+ "__agg_sum": 0
459
+ }
460
+ },
407
461
{
408
462
"$limit": 5000
409
463
}
0 commit comments