1
1
.. _aggregation-pipeline-generation:
2
2
3
- ===========================
4
- Charts Aggregation Pipeline
5
- ===========================
3
+ ============================
4
+ Backing Aggregation Pipeline
5
+ ============================
6
6
7
7
.. default-domain:: mongodb
8
8
9
- In order to get the data needed to render a chart, |charts-short| creates a MongoDB Aggregation Pipeline which is executed on the
10
- MongoDB database server. The pipeline consists of multiple stages, each of which is generated based on different settings specified
11
- by the chart's author. This document explains how the various Chart Builder settings are used to construct the Aggregation Pipeline.
12
- You can view the pipeline used to create a chart by choosing the :guilabel:`View Aggregation Pipeline` option in the Chart Builder's
13
- ellipsis dropdown on the top right.
9
+ To get the data needed to render a chart, |charts-short| creates a
10
+ MongoDB Aggregation Pipeline which is executed on the MongoDB database
11
+ server. The pipeline consists of multiple stages, each of which is
12
+ generated based on different settings specified by the chart's author.
13
+ This document explains how the various Chart Builder settings are used
14
+ to construct the Aggregation Pipeline. You can view the pipeline used to
15
+ create a chart by choosing the :guilabel:`View Aggregation Pipeline`
16
+ option in the Chart Builder's ellipsis dropdown on the top right.
14
17
15
18
16
19
The pipeline constructed by |charts| consists of the following segments in the following order:
@@ -25,23 +28,27 @@ The pipeline constructed by |charts| consists of the following segments in the f
25
28
#. Maximum Document limit
26
29
27
30
.. note::
28
- You do not need to configure all of the above settings when creating a chart. Unspecified settings are skipped when
29
- generating the aggregation pipeline.
31
+ You do not need to configure all of the above settings when creating
32
+ a chart. Unspecified settings are skipped when generating the
33
+ aggregation pipeline.
30
34
31
35
32
36
.. note::
33
- Sorts and limits specified in the encoding panel are currently not included in the aggregation pipeline. Instead, the sorting and limiting is
34
- applied on the client-side while rendering the chart.
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.
35
40
36
41
37
42
Example
38
43
-------
39
44
40
- The following chart shows the total sale amounts from an office supply company, categorized by
41
- purchase method. Each document in the data collection represents a single sale.
45
+ The following chart shows the total sale amounts from an office supply
46
+ company, categorized by purchase method. Each document in the data
47
+ collection represents a single sale.
42
48
43
- Using this chart as an example, we will explore how the specifications for each of the above settings change the
44
- aggregation pipeline generated by |charts|.
49
+ Using this chart as an example, we will explore how the specifications
50
+ for each of the above settings change the aggregation pipeline generated
51
+ by |charts|.
45
52
46
53
.. figure:: /images/charts/agg-pipeline.png
47
54
:figwidth: 720px
@@ -51,15 +58,15 @@ aggregation pipeline generated by |charts|.
51
58
Encoding
52
59
~~~~~~~~
53
60
54
- Without any :guilabel:`Data Source` pipeline, :guilabel:`Query` bar queries,
55
- calculated fields, and filters added in the :guilabel:`Filter` pane, |charts| generates the
56
- following aggregation pipeline:
61
+ Without any :guilabel:`Data Source` pipeline, :guilabel:`Query` bar
62
+ queries, calculated fields, and filters added in the :guilabel:`Filter`
63
+ pane, |charts| generates the following aggregation pipeline:
57
64
58
65
.. code-block:: javascript
59
66
:emphasize-lines: 1-31
60
67
61
68
{
62
- "$addFields": { //Encoding
69
+ "$addFields": { //Encoding
63
70
"__alias_0": {
64
71
"$sum": "$items.price"
65
72
}
@@ -94,8 +101,9 @@ following aggregation pipeline:
94
101
}
95
102
96
103
97
- The pipeline at this stage only consists of groups from the :guilabel:`Encode` panel and the maximum
98
- document limit, which is set to 5000 by |charts|.
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|.
99
107
100
108
Adding Queries
101
109
~~~~~~~~~~~~~~
@@ -121,7 +129,8 @@ Query:
121
129
}
122
130
123
131
124
- Applying the above query in the :guilabel:`Query` bar generates the following chart and aggregation pipeline:
132
+ Applying the above query in the :guilabel:`Query` bar generates the
133
+ following chart and aggregation pipeline:
125
134
126
135
.. figure:: /images/charts/agg-pipeline-query.png
127
136
:figwidth: 720px
@@ -133,7 +142,7 @@ Aggregation Pipeline:
133
142
:emphasize-lines: 1-18
134
143
135
144
{
136
- "$match": { // Query
145
+ "$match": { // Query
137
146
"$and": [
138
147
{
139
148
"saleDate": {
@@ -185,16 +194,18 @@ Aggregation Pipeline:
185
194
}
186
195
187
196
188
- The aggregation pipeline now starts with the query applied, and is followed by the groups selected in the
189
- :guilabel:`Encode` panel and the max document limit.
197
+ The aggregation pipeline now starts with the query applied, and is
198
+ followed by the groups selected in the :guilabel:`Encode` panel and the
199
+ max document limit.
190
200
191
201
192
202
Adding Calculated Fields
193
203
~~~~~~~~~~~~~~~~~~~~~~~~
194
204
195
- We can also change the chart to show the total revenue generated categorized by
196
- purchase method. To accomplish this task, we will create a calculated field that calculates
197
- the total revenue by multiplying price by quantity. Adding this new calculated field, in addition to the
205
+ We can also change the chart to show the total revenue generated
206
+ categorized by purchase method. To accomplish this task, we will create
207
+ a calculated field that calculates the total revenue by multiplying
208
+ price by quantity. Adding this new calculated field, in addition to the
198
209
query above, produces the following chart and pipeline:
199
210
200
211
@@ -230,7 +241,7 @@ Aggregation Pipeline:
230
241
}
231
242
},
232
243
{
233
- "$addFields": { // Calculated Field
244
+ "$addFields": { // Calculated Field
234
245
"revenue": {
235
246
"$reduce": {
236
247
"input": "$items",
@@ -279,15 +290,18 @@ Aggregation Pipeline:
279
290
}
280
291
281
292
282
- The updated pipeline now includes the calculated field right below the query applied in the :guilabel:`Query` bar while the order of
283
- the rest of the components remains unchanged.
293
+ The updated pipeline now includes the calculated field right below the
294
+ query applied in the :guilabel:`Query` bar while the order of the rest
295
+ of the components remains unchanged.
284
296
285
297
286
298
Adding Filters
287
299
~~~~~~~~~~~~~~
288
300
289
- This chart can be further refined by adding a filter in the :guilabel:`Filter` pane to only select in-store sales made in the New York location.
290
- Adding this filter produces the following chart and aggregation pipeline:
301
+ This chart can be further refined by adding a filter in the
302
+ :guilabel:`Filter` pane to only select in-store sales made in the New
303
+ York location. Adding this filter produces the following chart and
304
+ aggregation pipeline:
291
305
292
306
.. figure:: /images/charts/agg-pipeline-with-filter.png
293
307
:figwidth: 720px
@@ -339,7 +353,7 @@ Aggregation Pipeline:
339
353
}
340
354
},
341
355
{
342
- "$match": { // Filter
356
+ "$match": { // Filter
343
357
"storeLocation": {
344
358
"$nin": [
345
359
null,
@@ -394,5 +408,6 @@ Aggregation Pipeline:
394
408
"$limit": 5000
395
409
}
396
410
397
- The pipeline now includes the storeLocation filter right below the calculated field while the order of
398
- the rest of the components remains unchanged.
411
+ The pipeline now includes the ``storeLocation`` filter right below the
412
+ calculated field while the order of the rest of the components remains
413
+ unchanged.
0 commit comments