|
| 1 | +.. _convert-field-data-types: |
| 2 | + |
| 3 | +======================== |
| 4 | +Convert Field Data Types |
| 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 convert the data type of the fields returned from your data |
| 16 | +source in the Chart Builder to a different data type. For example, you |
| 17 | +can convert numbers stored as strings to numbers, convert dates stored |
| 18 | +as strings or Unix timestamps to dates, or treat ObjectId fields as |
| 19 | +dates. |
| 20 | + |
| 21 | +Requirements & Limitations |
| 22 | +-------------------------- |
| 23 | + |
| 24 | +- Your deployment must use MongoDB 4.0 or later. |
| 25 | + |
| 26 | +- You cannot convert GeoJSON fields. |
| 27 | + |
| 28 | +- You cannot convert arrays nested more than one level deep. |
| 29 | + |
| 30 | +Convert a Field's Data Type |
| 31 | +--------------------------- |
| 32 | + |
| 33 | +To convert a field's data type, click on that field and select |
| 34 | +:guilabel:`Convert type` from the :guilabel:`Ellipses (...)` menu. |
| 35 | + |
| 36 | +.. figure:: /images/charts/convert-field-data-type.png |
| 37 | + :figwidth: 25% |
| 38 | + :alt: To change a field's data type, click on that field and select "Convert type" from the ellipses menu. |
| 39 | + |
| 40 | +|charts| supports converting data into the following types: |
| 41 | + |
| 42 | +.. list-table:: |
| 43 | + :header-rows: 1 |
| 44 | + :widths: 50 50 |
| 45 | + |
| 46 | + * - Label |
| 47 | + - BSON Type |
| 48 | + |
| 49 | + * - :guilabel:`String` |
| 50 | + - :manual:`String </reference/operator/aggregation/convert/#convert-to-string>` |
| 51 | + |
| 52 | + * - :guilabel:`Date` |
| 53 | + - :manual:`Date </reference/operator/aggregation/convert/#convert-to-date>` |
| 54 | + |
| 55 | + * - :guilabel:`Number` |
| 56 | + - :manual:`Double </reference/operator/aggregation/convert/#convert-to-double>` |
| 57 | + |
| 58 | + * - :guilabel:`Boolean` |
| 59 | + - :manual:`Bool </reference/operator/aggregation/convert/#convert-to-bool>` |
| 60 | + |
| 61 | +Data types are converted by an |
| 62 | +:manual:`aggregation pipeline </core/aggregation-pipeline>`. |
| 63 | +For each type conversion you select, you can find the underlying |
| 64 | +aggregation stage by |
| 65 | +:ref:`viewing your aggregation pipeline <view-aggregation-pipeline>`. |
| 66 | + |
| 67 | +To see the effects of your conversion on a sample document, |
| 68 | +:ref:`view a sample source document <view-source-documents>`. |
| 69 | + |
| 70 | +Your conversions are saved with your chart definition. |
| 71 | + |
| 72 | +Convert Data Types in an Array |
| 73 | +------------------------------ |
| 74 | + |
| 75 | +You can convert arrays of primitive data and fields of primitive data |
| 76 | +from arrays of documents. Each item in the array is converted to the |
| 77 | +new type. However, if the conversion is invalid or if the original |
| 78 | +value of the item is ``null``, the item converts to ``null``. You |
| 79 | +cannot convert arrays nested more than one level deep. |
| 80 | + |
| 81 | +.. example:: |
| 82 | + |
| 83 | + You have an array of strings: |
| 84 | + |
| 85 | + .. code-block:: javascript |
| 86 | + :copyable: false |
| 87 | + |
| 88 | + ["1", "fish", "42"] |
| 89 | + |
| 90 | + Applying the :guilabel:`Number` data type conversion returns the |
| 91 | + following array: |
| 92 | + |
| 93 | + .. code-block:: javascript |
| 94 | + :copyable: false |
| 95 | + |
| 96 | + [1, null, 45] |
| 97 | + |
| 98 | +.. example:: |
| 99 | + |
| 100 | + You have an array of documents with fields ``foo`` and ``bar``: |
| 101 | + |
| 102 | + .. code-block:: javascript |
| 103 | + :copyable: false |
| 104 | + |
| 105 | + [{foo: "1", bar: "2"}, {foo: "fish", bar: "tacos"}, {foo: "45", |
| 106 | + bar: "46"}] |
| 107 | + |
| 108 | + Applying the :guilabel:`Number` data type conversion to the ``foo`` |
| 109 | + field returns the following array of documents: |
| 110 | + |
| 111 | + .. code-block:: javascript |
| 112 | + :copyable: false |
| 113 | + |
| 114 | + [{foo: 1, bar: "2"}, {foo: null, bar: "tacos"}, {foo: 45, bar: |
| 115 | + "46"}] |
| 116 | + |
| 117 | +Revert a Field's Data Type Conversion |
| 118 | +------------------------------------- |
| 119 | + |
| 120 | +A converted field's icon reflects the new data type and turns green. |
| 121 | +To remove a conversion and return the field to its original data type, |
| 122 | +select :guilabel:`Auto Detect` from the :guilabel:`Ellipses (...)` |
| 123 | +:icon-fa5:`arrow-right` :guilabel:`Convert Type` menu. |
| 124 | + |
| 125 | +.. figure:: /images/charts/converted-field-icon-example.png |
| 126 | + :figwidth: 70% |
| 127 | + :alt: A field converted to a string will show a green "A" symbol to indicate its new status. |
0 commit comments