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.

Convert Field Data Types

You can convert the data type of the fields returned from your data source in the Chart Builder to a different data type. For example, you can convert numbers stored as strings to numbers, convert dates stored as strings or Unix timestamps to dates, or treat ObjectId fields as dates.

Requirements & Limitations

  • Your deployment must use MongoDB 4.0 or later.
  • You cannot convert GeoJSON fields.
  • You cannot convert arrays nested more than one level deep.

Convert a Field’s Data Type

To convert a field’s data type, click on that field and select Convert type from the Ellipses (…) menu.

To change a field's data type, click on that field and select "Convert type" from the ellipses menu.

MongoDB Charts supports converting data into the following types:

Label BSON Type
String String
Date Date
Number Double
Boolean Bool

Data types are converted by an aggregation pipeline. For each type conversion you select, you can find the underlying aggregation stage by viewing your aggregation pipeline.

To see the effects of your conversion on a sample document, view a sample source document.

Your conversions are saved with your chart definition.

Convert Data Types in an Array

You can convert arrays of primitive data and fields of primitive data from arrays of documents. Each item in the array is converted to the new type. However, if the conversion is invalid or if the original value of the item is null, the item converts to null. You cannot convert arrays nested more than one level deep.

Example

You have an array of strings:

["1", "fish", "42"]

Applying the Number data type conversion returns the following array:

[1, null, 45]

Example

You have an array of documents with fields foo and bar:

[{foo: "1", bar: "2"}, {foo: "fish", bar: "tacos"}, {foo: "45",
bar: "46"}]

Applying the Number data type conversion to the foo field returns the following array of documents:

[{foo: 1, bar: "2"}, {foo: null, bar: "tacos"}, {foo: 45, bar:
"46"}]

Revert a Field’s Data Type Conversion

A converted field’s icon reflects the new data type and turns green. To remove a conversion and return the field to its original data type, select Auto Detect from the Ellipses (…) arrow right icon Convert Type menu.

A field converted to a string will show a green "A" symbol to indicate its new status.