Skip to content

Commit 0692246

Browse files
(DOCSP-7488): Missed & Calculated Fields (#269)
* (DOCSP-4788): Missed & Calculated Fields * (DOCSP-7488): Add screenshot * (DOCSP-7488): Small fixes * (DOCSP-7488): Copy & tech review * (DOCSP-7488): Fix screenshot, rearrange
1 parent fde22ae commit 0692246

File tree

8 files changed

+253
-15
lines changed

8 files changed

+253
-15
lines changed

source/build-charts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Create a Chart
2929
/view-export-chart-data
3030
/convert-field-data-types
3131
/encoding-channels
32+
/calculated-fields
3233
/multi-series-charts
3334
/filter-documents
3435
/rich-schema-support

source/calculated-fields.txt

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
.. _calculated-fields:
2+
3+
=================
4+
Calculated Fields
5+
=================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
You can combine the data from one or more fields in your collection into
16+
a single calculated field. For example, you can:
17+
18+
- Convert a field in hours to seconds or in degrees Farenheit to Celsius
19+
- Multiply a price field by a quantity field to create a total
20+
- Combine multiple line items within an array to calculate a total
21+
22+
Considerations
23+
--------------
24+
25+
- You can only create calculated fields from data within the same
26+
document.
27+
- You can't :ref:`convert the type <convert-field-data-types>` of a
28+
calculated field through the |charts| interface. However, you can use
29+
:manual:`Type Expression Operators
30+
</meta/aggregation-quick-reference/#type-expression-operators>` in a
31+
calculated field's definition.
32+
- Once you create a calculated field, you can :ref:`modify
33+
<edit-calculated-field>` its definition but not its name. However, you
34+
can :ref:`remove <remove-calculated-field>` and recreate the field if
35+
you need to rename it.
36+
37+
.. _create-calculated-field:
38+
39+
Create a Calculated Field
40+
-------------------------
41+
42+
You create a calculated field by combining the data of existing fields
43+
through simple expressions or :abbr:`MQL (MongoDB Query Language)`
44+
:ref:`agg-quick-ref-operator-expressions`.
45+
46+
.. note::
47+
48+
The definition of a calculated field can contain either simple
49+
expression language or operator expression language. You can't use
50+
both simple and operator expression language in the same definition.
51+
52+
To create a calculated field:
53+
54+
1. In the corner of the :guilabel:`Fields` pane, click
55+
:icon-fa5:`plus`:guilabel:`Add Field`.
56+
57+
#. Select :guilabel:`Calculated`.
58+
59+
#. Enter the :guilabel:`Field Name` of the calculated field you want to
60+
define. You can specify a nested field by using dot notation. For
61+
example, ``metadata.target``.
62+
63+
#. Enter the :guilabel:`Value Expression` using :ref:`simple expression
64+
<calc-field-simple>` language or
65+
:ref:`agg-quick-ref-operator-expressions`.
66+
67+
#. Click :guilabel:`Save Field`.
68+
69+
The calculated field appears in italics in the :guilabel:`Fields` pane.
70+
71+
.. _calc-field-simple:
72+
73+
Simple Expressions
74+
~~~~~~~~~~~~~~~~~~
75+
76+
You can use the following simple expression language in a calculated
77+
field.
78+
79+
.. list-table::
80+
:header-rows: 1
81+
:widths: 50 50
82+
83+
* - Expression Language
84+
- Example
85+
86+
* - Field names
87+
- | ``orderTotal``
88+
| ``'orderTotal'``
89+
| ``"orderTotal"``
90+
91+
* - Literal numbers
92+
- | ``2``
93+
| ``0.5``
94+
95+
* - Mathematical operators
96+
- | ``+``
97+
| ``-``
98+
| ``*``
99+
| ``/``
100+
101+
* - Brackets
102+
- ``( )``
103+
104+
Whitespace that is outside of quoted strings is not included in the
105+
expression.
106+
107+
.. figure:: /images/charts/calculated-field-simple.png
108+
:figwidth: 75%
109+
:alt: Click "Add Field", enter a field name and simple expressions definition, then click "Save Field".
110+
111+
.. example::
112+
113+
The following examples are valid simple expressions to define a
114+
calculated field.
115+
116+
Add the ``bathrooms`` field to the ``bedrooms`` field:
117+
118+
.. code-block:: json
119+
120+
bathrooms + bedrooms
121+
122+
Multiply the ``total amount`` field by 1.1:
123+
124+
.. code-block:: json
125+
126+
'total amount' * 1.1
127+
128+
Subtract ``32`` from the ``sensor.temp`` field, then multiply by
129+
``5`` and divide by ``9``:
130+
131+
.. code-block:: json
132+
133+
(sensor.temp - 32)*5/9
134+
135+
.. _calc-field-mql:
136+
137+
Operator Expressions
138+
~~~~~~~~~~~~~~~~~~~~
139+
140+
You can use :ref:`agg-quick-ref-operator-expressions` to define more
141+
complex calculated fields.
142+
143+
.. example::
144+
145+
The following examples are valid operator expressions to define a
146+
calculated field.
147+
148+
Multiply the ``price`` field by ``0.075``:
149+
150+
.. code-block:: json
151+
152+
{ $multiply: [ "$price", 0.075 ] }
153+
154+
Combine multiple line items fields in an array to calculate a
155+
total:
156+
157+
.. code-block:: json
158+
159+
{ $reduce: {
160+
input: '$items', initialValue: 0,
161+
in: { $sum : ["$$value",
162+
{ $multiply: ["$$this.price",
163+
"$$this.quantity"] }
164+
] } } }
165+
166+
.. tip::
167+
168+
You can use a calculated field in the definition of another
169+
calculated field.
170+
171+
For example, if you create a ``salesTax`` calculated field with the
172+
following definition:
173+
174+
.. code-block:: json
175+
176+
{ $multiply: [ "$price", 0.075 ] }
177+
178+
You can then create a ``totalCost`` calculated field that uses the
179+
``salesTax`` field in its definition:
180+
181+
.. code-block:: json
182+
183+
{ $sum: ["$price", "$salesTax"] }
184+
185+
.. _edit-calculated-field:
186+
187+
Edit a Calculated Field
188+
-----------------------
189+
190+
You can modify the definition of a calculated field.
191+
192+
1. In the :guilabel:`Fields` pane, click the :guilabel:`Ellipsis (...)`
193+
next to the name of the calculated field you want to modify.
194+
195+
#. Select :guilabel:`Modify field`.
196+
197+
#. Update the :guilabel:`Value Expression`.
198+
199+
#. Click :guilabel:`Save Field`.
200+
201+
If you are using the calculated field in a chart, the chart refreshes to
202+
reflect the new calculated field definition.
203+
204+
.. figure:: /images/charts/calculated-field-modify.png
205+
:figwidth: 50%
206+
:alt: Click the ellipsis next to the field name, then click "Modify field".
207+
208+
.. _remove-calculated-field:
209+
210+
Remove a Calculated Field
211+
-------------------------
212+
213+
When you remove a calculated field, |charts| resamples the data source,
214+
updates the :guilabel:`Fields` pane, and refreshes your chart. If the
215+
calculated field was used in encodings or filters, it remains in the
216+
chart even after the field is removed from the :guilabel:`Fields` pane.
217+
218+
To remove a calculated field:
219+
220+
1. In the :guilabel:`Fields` pane, click the :guilabel:`Ellipsis (...)`
221+
next to the name of the calculated field you want to remove.
222+
223+
#. Select :guilabel:`Remove field`.
224+
225+
.. figure:: /images/charts/calculated-field-remove.png
226+
:figwidth: 50%
227+
:alt: Click the ellipsis next to the field name, then click "Remove field".

source/encoding-channels.txt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,29 +166,39 @@ For additional information and examples on :guilabel:`Geospatial`
166166
:guilabel:`Scatter` and :guilabel:`Heatmap` charts which utilize
167167
geopoint channel types, refer to the relevant chart type pages.
168168

169-
Surface Missing Fields in the Chart Builder
170-
-------------------------------------------
169+
Add Missing Fields in the Chart Builder
170+
---------------------------------------
171171

172172
|charts| populates the :guilabel:`Fields` pane of the
173173
chart builder by randomly sampling documents from the selected
174-
data source. As a result, |charts-short| may not display all fields
174+
data source. As a result, |charts-short| might not display all fields
175175
from documents in the data source if the field is not present on all
176176
documents.
177177

178-
To surface a specific field in the chart builder, you can use the
179-
:ref:`Query Bar <query-bar>` to define certain fields which must
180-
appear in the sampled documents.
178+
To add a specific field in the chart builder:
181179

182-
.. example::
180+
1. In the corner of the :guilabel:`Fields` pane, click
181+
:icon-fa5:`plus`:guilabel:`Add Field`.
182+
183+
#. Make sure that the default :guilabel:`Missed` field type is selected.
184+
185+
#. Enter the :guilabel:`Field Name` of the field you want to
186+
add.
183187

184-
The following MQL query, when executed in the query bar,
185-
ensures that the ``customer_satisfaction`` field is included
186-
in the :guilabel:`Fields` pane.
188+
.. note::
189+
190+
You can specify a nested field by using dot notation. For example,
191+
you can specify ``address.neighborhood``.
187192

188-
.. code-block:: json
193+
#. Click :guilabel:`Save Field`.
189194

190-
{ "customer_satisfaction": { $exists: true } }
195+
.. figure:: /images/charts/missed-field-add.png
196+
:figwidth: 75%
197+
:alt: To add a missed field, click "Add Field".
191198

192-
This query guarantees that you can interact with the
193-
``customer_satisfaction`` field and include the field in chart
194-
construction.
199+
After |charts| locates the missed field, the field appears in italics
200+
the :guilabel:`Fields` pane. If |charts| discovers other missed
201+
fields in the same subdocument, |charts| also adds those fields in
202+
italics. Once you add a field, you can include it in your chart,
203+
:doc:`convert its data type </convert-field-data-types>`, or
204+
:ref:`remove it <remove-calculated-field>`.
24.3 KB
Loading
116 KB
Loading
24.6 KB
Loading
107 KB
Loading
89.8 KB
Loading

0 commit comments

Comments
 (0)