Skip to content

Commit 18d898d

Browse files
zach-carrjeff-allen-mongo
authored andcommitted
(DOCSP-11492) Move sorting and limiting into agg pipeline (#396)
(DOCSP-11492) Move sorting and limiting into agg pipeline
1 parent 5781bac commit 18d898d

File tree

1 file changed

+103
-49
lines changed

1 file changed

+103
-49
lines changed

source/aggregation-pipeline-generation.txt

Lines changed: 103 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,18 @@ The pipeline constructed by |charts| consists of the following segments in the f
2020

2121
1. `Data Source Pipeline <https://docs.mongodb.com/charts/saas/data-source-pipeline/>`_
2222
#. `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>`_
2424
#. `Embedding Filters <https://docs.mongodb.com/charts/saas/embedded-chart-options/#embedded-chart-options>`_
2525
#. `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>`_
2727
#. `Encoding <https://docs.mongodb.com/charts/saas/encoding-channels/>`_
28+
#. `Sorting & Limiting <https://docs.mongodb.com/charts/saas/bin-data/#sort-data>`_
2829
#. Maximum Document limit
2930

3031
.. note::
3132
You do not need to configure all of the above settings when creating
3233
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.
4135

4236
Example
4337
-------
@@ -63,10 +57,11 @@ queries, calculated fields, and filters added in the :guilabel:`Filter`
6357
pane, |charts| generates the following aggregation pipeline:
6458

6559
.. code-block:: javascript
66-
:emphasize-lines: 1-31
60+
:emphasize-lines: 1-31, 33-51
61+
:linenos:
6762

6863
{
69-
"$addFields": { //Encoding
64+
"$addFields": { // Encoding
7065
"__alias_0": {
7166
"$sum": "$items.price"
7267
}
@@ -91,19 +86,38 @@ pane, |charts| generates the following aggregation pipeline:
9186
},
9287
{
9388
"$project": {
94-
"y": "$__alias_0",
9589
"x": "$__alias_1",
90+
"y": "$__alias_0",
9691
"_id": 0
9792
}
9893
},
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+
},
99114
{
100115
"$limit": 5000
101116
}
102-
103117

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|.
107121

108122
Adding Queries
109123
~~~~~~~~~~~~~~
@@ -140,9 +154,10 @@ Aggregation Pipeline:
140154

141155
.. code-block:: javascript
142156
:emphasize-lines: 1-18
157+
:linenos:
143158

144159
{
145-
"$match": { // Query
160+
"$match": { // Query
146161
"$and": [
147162
{
148163
"saleDate": {
@@ -164,15 +179,16 @@ Aggregation Pipeline:
164179
"__alias_0": {
165180
"$sum": "$items.price"
166181
}
167-
},
182+
}
183+
},
168184
{
169185
"$group": {
170186
"_id": {
171187
"__alias_1": "$purchaseMethod"
172188
},
173189
"__alias_0": {
174190
"$sum": "$__alias_0"
175-
}
191+
}
176192
}
177193
},
178194
{
@@ -184,16 +200,34 @@ Aggregation Pipeline:
184200
},
185201
{
186202
"$project": {
187-
"y": "$__alias_0",
188203
"x": "$__alias_1",
204+
"y": "$__alias_0",
189205
"_id": 0
190206
}
191207
},
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+
},
192227
{
193228
"$limit": 5000
194229
}
195230

196-
197231
The aggregation pipeline now starts with the query applied, and is
198232
followed by the groups selected in the :guilabel:`Encode` panel and the
199233
max document limit.
@@ -221,6 +255,7 @@ Aggregation Pipeline:
221255

222256
.. code-block:: javascript
223257
:emphasize-lines: 19-39
258+
:linenos:
224259

225260
{
226261
"$match": {
@@ -241,7 +276,7 @@ Aggregation Pipeline:
241276
}
242277
},
243278
{
244-
"$addFields": { // Calculated Field
279+
"$addFields": { // Calculated Field
245280
"revenue": {
246281
"$reduce": {
247282
"input": "$items",
@@ -285,16 +320,34 @@ Aggregation Pipeline:
285320
"_id": 0
286321
}
287322
},
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+
},
288342
{
289343
"$limit": 5000
290344
}
291-
345+
292346

293347
The updated pipeline now includes the calculated field right below the
294348
query applied in the :guilabel:`Query` bar while the order of the rest
295349
of the components remains unchanged.
296350

297-
298351
Adding Filters
299352
~~~~~~~~~~~~~~
300353

@@ -311,7 +364,8 @@ aggregation pipeline:
311364
Aggregation Pipeline:
312365

313366
.. code-block:: javascript
314-
:emphasize-lines: 40-54
367+
:emphasize-lines: 40-48
368+
:linenos:
315369

316370
{
317371
"$match": {
@@ -353,16 +407,10 @@ Aggregation Pipeline:
353407
}
354408
},
355409
{
356-
"$match": { // Filter
410+
"$match": { // Filter
357411
"storeLocation": {
358-
"$nin": [
359-
null,
360-
"",
361-
"Austin",
362-
"Denver",
363-
"London",
364-
"San Diego",
365-
"Seattle"
412+
"$in": [
413+
"New York"
366414
]
367415
}
368416
}
@@ -373,20 +421,7 @@ Aggregation Pipeline:
373421
"__alias_0": "$purchaseMethod"
374422
},
375423
"__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"
390425
}
391426
}
392427
},
@@ -404,6 +439,25 @@ Aggregation Pipeline:
404439
"_id": 0
405440
}
406441
},
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+
},
407461
{
408462
"$limit": 5000
409463
}

0 commit comments

Comments
 (0)