|
| 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". |
0 commit comments