11.. _aggregation-pipeline-generation:
22
3- ===========================
4- Charts Aggregation Pipeline
5- ===========================
3+ ============================
4+ Backing Aggregation Pipeline
5+ ============================
66
77.. default-domain:: mongodb
88
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.
1417
1518
1619The 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
2528#. Maximum Document limit
2629
2730.. 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.
3034
3135
3236.. 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.
3540
3641
3742Example
3843-------
3944
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.
4248
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|.
4552
4653.. figure:: /images/charts/agg-pipeline.png
4754 :figwidth: 720px
@@ -51,15 +58,15 @@ aggregation pipeline generated by |charts|.
5158Encoding
5259~~~~~~~~
5360
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:
5764
5865.. code-block:: javascript
5966 :emphasize-lines: 1-31
6067
6168 {
62- "$addFields": { //Encoding
69+ "$addFields": { //Encoding
6370 "__alias_0": {
6471 "$sum": "$items.price"
6572 }
@@ -94,8 +101,9 @@ following aggregation pipeline:
94101 }
95102
96103
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|.
99107
100108Adding Queries
101109~~~~~~~~~~~~~~
@@ -121,7 +129,8 @@ Query:
121129 }
122130
123131
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:
125134
126135.. figure:: /images/charts/agg-pipeline-query.png
127136 :figwidth: 720px
@@ -133,7 +142,7 @@ Aggregation Pipeline:
133142 :emphasize-lines: 1-18
134143
135144 {
136- "$match": { // Query
145+ "$match": { // Query
137146 "$and": [
138147 {
139148 "saleDate": {
@@ -185,16 +194,18 @@ Aggregation Pipeline:
185194 }
186195
187196
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.
190200
191201
192202Adding Calculated Fields
193203~~~~~~~~~~~~~~~~~~~~~~~~
194204
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
198209query above, produces the following chart and pipeline:
199210
200211
@@ -230,7 +241,7 @@ Aggregation Pipeline:
230241 }
231242 },
232243 {
233- "$addFields": { // Calculated Field
244+ "$addFields": { // Calculated Field
234245 "revenue": {
235246 "$reduce": {
236247 "input": "$items",
@@ -279,15 +290,18 @@ Aggregation Pipeline:
279290 }
280291
281292
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.
284296
285297
286298Adding Filters
287299~~~~~~~~~~~~~~
288300
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:
291305
292306.. figure:: /images/charts/agg-pipeline-with-filter.png
293307 :figwidth: 720px
@@ -339,7 +353,7 @@ Aggregation Pipeline:
339353 }
340354 },
341355 {
342- "$match": { // Filter
356+ "$match": { // Filter
343357 "storeLocation": {
344358 "$nin": [
345359 null,
@@ -394,5 +408,6 @@ Aggregation Pipeline:
394408 "$limit": 5000
395409 }
396410
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