Navigation
This documentation refers to the MongoDB Charts service in MongoDB Atlas. Read the on-premises documentation to learn how to use the MongoDB Charts on site.

Calculated Fields

You can combine the data from one or more fields in your collection into a single calculated field. For example, you can:

  • Convert a field in hours to seconds or in degrees Farenheit to Celsius
  • Multiply a price field by a quantity field to create a total
  • Combine multiple line items within an array to calculate a total

Considerations

  • You can only create calculated fields from data within the same document.
  • You can’t convert the type of a calculated field through the MongoDB Charts interface. However, you can use Type Expression Operators in a calculated field’s definition.
  • Once you create a calculated field, you can modify its definition but not its name. However, you can remove and recreate the field if you need to rename it.

Create a Calculated Field

You create a calculated field by combining the data of existing fields through simple expressions or MQL Operator Expressions.

Note

The definition of a calculated field can contain either simple expression language or operator expression language. You can’t use both simple and operator expression language in the same definition.

To create a calculated field:

  1. In the corner of the Fields pane, click plus icon Add Field.
  2. Select Calculated.
  3. Enter the Field Name of the calculated field you want to define. You can specify a nested field by using dot notation. For example, metadata.target.
  4. Enter the Value Expression using simple expression language or Operator Expressions.
  5. Click Save Field.

The calculated field appears in italics in the Fields pane.

Simple Expressions

You can use the following simple expression language in a calculated field.

Expression Language Example
Field names
orderTotal
'orderTotal'
"orderTotal"
Literal numbers
2
0.5
Mathematical operators
+
-
*
/
Brackets ( )

Whitespace that is outside of quoted strings is not included in the expression.

Click "Add Field", enter a field name and simple expressions definition, then click "Save Field".

Example

The following examples are valid simple expressions to define a calculated field.

Add the bathrooms field to the bedrooms field:

bathrooms + bedrooms

Multiply the total amount field by 1.1:

'total amount' * 1.1

Subtract 32 from the sensor.temp field, then multiply by 5 and divide by 9:

(sensor.temp - 32)*5/9

Operator Expressions

You can use Operator Expressions to define more complex calculated fields.

Example

The following examples are valid operator expressions to define a calculated field.

Multiply the price field by 0.075:

{ $multiply: [ "$price", 0.075 ] }

Combine multiple line items fields in an array to calculate a total:

{ $reduce: {
  input: '$items', initialValue: 0,
  in: { $sum : ["$$value",
    { $multiply: ["$$this.price",
      "$$this.quantity"] }
  ] } } }

Tip

You can use a calculated field in the definition of another calculated field.

For example, if you create a salesTax calculated field with the following definition:

{ $multiply: [ "$price", 0.075 ] }

You can then create a totalCost calculated field that uses the salesTax field in its definition:

{ $sum: ["$price", "$salesTax"] }

Edit a Calculated Field

You can modify the definition of a calculated field.

  1. In the Fields pane, click the Ellipsis (…) next to the name of the calculated field you want to modify.
  2. Select Modify field.
  3. Update the Value Expression.
  4. Click Save Field.

If you are using the calculated field in a chart, the chart refreshes to reflect the new calculated field definition.

Click the ellipsis next to the field name, then click "Modify field".

Remove a Calculated Field

When you remove a calculated field, MongoDB Charts resamples the data source, updates the Fields pane, and refreshes your chart. If the calculated field was used in encodings or filters, it remains in the chart even after the field is removed from the Fields pane.

To remove a calculated field:

  1. In the Fields pane, click the Ellipsis (…) next to the name of the calculated field you want to remove.
  2. Select Remove field.
Click the ellipsis next to the field name, then click "Remove field".