Docs Menu

Calculate New Data Fields

On this page

  • Considerations
  • Create a Calculated Field
  • Edit a Calculated Field
  • Remove a Calculated Field

You can combine 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
  • 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.

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


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 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,
  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.

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

Expression Language
Field names
Literal numbers
Mathematical operators
( )

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".

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

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


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"] }
] } } }

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"] }

You can set a calculated field's value to a string using the $literal operator.


The following field definition assigns a value of my string:

{ $literal: "my string" }

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".

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".
←  Reshape Your DataConvert Field Data Types →
Give Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.