|
| 1 | +========================= |
| 2 | +Data Types in ``mongosh`` |
| 3 | +========================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +MongoDB :term:`BSON` provides support for additional data types than |
| 14 | +:term:`JSON`. :ecosystem:`Drivers </drivers>` provide native support for |
| 15 | +these data types in host languages and ``mongosh`` also provides several |
| 16 | +helper classes to support the use of these data types. See the |
| 17 | +:manual:`Extended JSON </reference/mongodb-extended-json>` reference for |
| 18 | +additional information. |
| 19 | + |
| 20 | +.. _mongo-shell-data-type: |
| 21 | + |
| 22 | +Types |
| 23 | +----- |
| 24 | + |
| 25 | +.. _mongo-shell-date-type: |
| 26 | + |
| 27 | +Date |
| 28 | +~~~~ |
| 29 | + |
| 30 | +``mongosh`` provides various methods to return the date, either as a |
| 31 | +string or as a ``Date`` object: |
| 32 | + |
| 33 | +- ``Date()`` method which returns the current date as a string. |
| 34 | + |
| 35 | +- ``new Date()`` constructor which returns a ``Date`` object using the |
| 36 | + ``ISODate()`` wrapper. |
| 37 | + |
| 38 | +- ``ISODate()`` constructor which returns a ``Date`` object using the |
| 39 | + ``ISODate()`` wrapper. |
| 40 | + |
| 41 | +Return Date as a String |
| 42 | +``````````````````````` |
| 43 | + |
| 44 | +To return the date as a string, use the ``Date()`` method, as in the |
| 45 | +following example: |
| 46 | + |
| 47 | +.. code-block:: javascript |
| 48 | + |
| 49 | + var myDateString = Date(); |
| 50 | + |
| 51 | +To print the value of the variable, type the variable name in the |
| 52 | +shell, as in the following: |
| 53 | + |
| 54 | +.. code-block:: javascript |
| 55 | + |
| 56 | + myDateString |
| 57 | + |
| 58 | +The result is the value of ``myDateString``: |
| 59 | + |
| 60 | +.. code-block:: javascript |
| 61 | + |
| 62 | + Wed Dec 19 2012 01:03:25 GMT-0500 (EST) |
| 63 | + |
| 64 | +To verify the type, use the ``typeof`` operator, as in the following: |
| 65 | + |
| 66 | +.. code-block:: javascript |
| 67 | + |
| 68 | + typeof myDateString |
| 69 | + |
| 70 | +The operation returns ``string``. |
| 71 | + |
| 72 | +Return ``Date`` |
| 73 | +``````````````` |
| 74 | + |
| 75 | +``mongosh`` wraps objects of ``Date`` type with the |
| 76 | +``ISODate`` helper; however, the objects remain of type ``Date``. |
| 77 | + |
| 78 | +The following example uses both the ``new Date()`` constructor and the |
| 79 | +``ISODate()`` constructor to return ``Date`` objects. |
| 80 | + |
| 81 | +.. code-block:: javascript |
| 82 | + |
| 83 | + var myDate = new Date(); |
| 84 | + var myDateInitUsingISODateWrapper = ISODate(); |
| 85 | + |
| 86 | +You can use the ``new`` operator with the ``ISODate()`` constructor as |
| 87 | +well. |
| 88 | + |
| 89 | +To print the value of the variable, type the variable name in the |
| 90 | +shell, as in the following: |
| 91 | + |
| 92 | +.. code-block:: javascript |
| 93 | + |
| 94 | + myDate |
| 95 | + |
| 96 | +The result is the ``Date`` value of ``myDate`` wrapped in the |
| 97 | +``ISODate()`` helper: |
| 98 | + |
| 99 | +.. code-block:: javascript |
| 100 | + |
| 101 | + ISODate("2012-12-19T06:01:17.171Z") |
| 102 | + |
| 103 | +To verify the type, use the ``instanceof`` operator, as in the |
| 104 | +following: |
| 105 | + |
| 106 | +.. code-block:: javascript |
| 107 | + |
| 108 | + myDate instanceof Date |
| 109 | + myDateInitUsingISODateWrapper instanceof Date |
| 110 | + |
| 111 | +The operation returns ``true`` for both. |
| 112 | + |
| 113 | +ObjectId |
| 114 | +~~~~~~~~ |
| 115 | + |
| 116 | +``mongosh`` provides the ``ObjectId()`` wrapper class around the |
| 117 | +:ref:`objectid` data type. To generate a new ObjectId, use the following |
| 118 | +operation in ``mongosh``: |
| 119 | + |
| 120 | +.. code-block:: javascript |
| 121 | + |
| 122 | + new ObjectId |
| 123 | + |
| 124 | +.. seealso:: |
| 125 | + |
| 126 | + :method:`ObjectId` |
| 127 | + |
| 128 | +.. _shell-type-long: |
| 129 | + |
| 130 | +NumberLong |
| 131 | +~~~~~~~~~~ |
| 132 | + |
| 133 | +``mongosh`` treats all numbers as floating-point values by default. |
| 134 | +``mongosh`` provides the ``NumberLong()`` wrapper to handle 64-bit |
| 135 | +integers. |
| 136 | + |
| 137 | +The ``NumberLong()`` wrapper accepts the long as a string: |
| 138 | + |
| 139 | +.. code-block:: javascript |
| 140 | + |
| 141 | + NumberLong("2090845886852") |
| 142 | + |
| 143 | +The following examples use the ``NumberLong()`` wrapper to write to the |
| 144 | +collection: |
| 145 | + |
| 146 | +.. code-block:: javascript |
| 147 | + |
| 148 | + db.collection.insertOne( { _id: 10, calc: NumberLong("2090845886852") } ) |
| 149 | + db.collection.updateOne( { _id: 10 }, |
| 150 | + { $set: { calc: NumberLong("2555555000000") } } ) |
| 151 | + db.collection.updateOne( { _id: 10 }, |
| 152 | + { $inc: { calc: NumberLong("5") } } ) |
| 153 | + |
| 154 | +Retrieve the document to verify: |
| 155 | + |
| 156 | +.. code-block:: javascript |
| 157 | + |
| 158 | + db.collection.findOne( { _id: 10 } ) |
| 159 | + |
| 160 | +In the returned document, the ``calc`` field contains a |
| 161 | +``NumberLong`` object: |
| 162 | + |
| 163 | +.. code-block:: sh |
| 164 | + |
| 165 | + { "_id" : 10, "calc" : NumberLong("2555555000005") } |
| 166 | + |
| 167 | +If you use the :update:`$inc` to increment the value of a field that |
| 168 | +contains a ``NumberLong`` object by a **float**, the data type changes |
| 169 | +to a floating point value, as in the following example: |
| 170 | + |
| 171 | +#. Use :update:`$inc` to increment the ``calc`` field by ``5``, which |
| 172 | + ``mongosh`` treats as a float: |
| 173 | + |
| 174 | + .. code-block:: javascript |
| 175 | + |
| 176 | + db.collection.updateOne( { _id: 10 }, |
| 177 | + { $inc: { calc: 5 } } ) |
| 178 | + |
| 179 | +#. Retrieve the updated document: |
| 180 | + |
| 181 | + .. code-block:: javascript |
| 182 | + |
| 183 | + db.collection.findOne( { _id: 10 } ) |
| 184 | + |
| 185 | + In the updated document, the ``calc`` field contains a floating |
| 186 | + point value: |
| 187 | + |
| 188 | + .. code-block:: sh |
| 189 | + |
| 190 | + { "_id" : 10, "calc" : 2555555000010 } |
| 191 | + |
| 192 | +.. note:: |
| 193 | + |
| 194 | + In the legacy :binary:`~bin.mongo` shell, ``NumberLong`` can accept |
| 195 | + either a string or integer value. In ``mongosh``, ``NumberLong`` can |
| 196 | + only accept a string value. |
| 197 | + |
| 198 | +.. _shell-type-int: |
| 199 | + |
| 200 | +NumberInt |
| 201 | +~~~~~~~~~ |
| 202 | + |
| 203 | +``mongosh`` treats all numbers as floating-point values by default. |
| 204 | +``mongosh`` provides the ``NumberInt()`` constructor to explicitly |
| 205 | +specify 32-bit integers. |
| 206 | + |
| 207 | +.. _shell-type-decimal: |
| 208 | + |
| 209 | +NumberDecimal |
| 210 | +~~~~~~~~~~~~~ |
| 211 | + |
| 212 | +.. versionadded:: 3.4 |
| 213 | + |
| 214 | +``mongosh`` treats all numbers as 64-bit floating-point ``double`` |
| 215 | +values by default. ``mongosh`` provides the ``NumberDecimal()`` |
| 216 | +constructor to explicitly specify 128-bit decimal-based floating-point |
| 217 | +values capable of emulating decimal rounding with exact precision. This |
| 218 | +functionality is intended for applications that handle :manual:`monetary |
| 219 | +data </tutorial/model-monetary-data>`, such as financial, tax, and |
| 220 | +scientific computations. |
| 221 | + |
| 222 | +The ``decimal`` :manual:`BSON type </reference/bson-types>` |
| 223 | +uses the IEEE 754 decimal128 floating-point numbering format which |
| 224 | +supports 34 decimal digits (i.e. significant digits) and an exponent |
| 225 | +range of −6143 to +6144. |
| 226 | + |
| 227 | +The ``NumberDecimal()`` constructor accepts the ``decimal`` value as a |
| 228 | +string: |
| 229 | + |
| 230 | +.. code-block:: javascript |
| 231 | + |
| 232 | + NumberDecimal("1000.55") |
| 233 | + |
| 234 | +The value is stored in the database as follows: |
| 235 | + |
| 236 | +.. code-block:: javascript |
| 237 | + |
| 238 | + NumberDecimal("1000.55") |
| 239 | + |
| 240 | +.. note:: |
| 241 | + |
| 242 | + In the legacy :binary:`~bin.mongo` shell, ``NumberLong`` can accept |
| 243 | + either a string or double value. In ``mongosh``, ``NumberLong`` can |
| 244 | + only accept a string value. |
| 245 | + |
| 246 | +.. note:: To use the ``decimal`` data type with a |
| 247 | + :ecosystem:`MongoDB driver </drivers/>`, be sure to use a driver |
| 248 | + version that supports it. |
| 249 | + |
| 250 | +Equality and Sort Order |
| 251 | +``````````````````````` |
| 252 | + |
| 253 | +Values of the ``decimal`` type are compared and sorted with other |
| 254 | +numeric types based on their actual numeric value. Numeric values |
| 255 | +of the binary-based ``double`` type generally have approximate |
| 256 | +representations of decimal-based values and may not be exactly |
| 257 | +equal to their ``decimal`` representations, so use the |
| 258 | +``NumberDecimal()`` constructor when checking the equality of |
| 259 | +``decimal`` values. Consider the following examples with the following |
| 260 | +documents in the ``numbers`` collection: |
| 261 | + |
| 262 | +.. code-block:: javascript |
| 263 | + |
| 264 | + { "_id" : 1, "val" : NumberDecimal( "9.99" ), "description" : "Decimal" } |
| 265 | + { "_id" : 2, "val" : 9.99, "description" : "Double" } |
| 266 | + { "_id" : 3, "val" : 10, "description" : "Double" } |
| 267 | + { "_id" : 4, "val" : NumberLong("10"), "description" : "Long" } |
| 268 | + { "_id" : 5, "val" : NumberDecimal( "10.0" ), "description" : "Decimal" } |
| 269 | + |
| 270 | +When the queries from the table below are plugged into the |
| 271 | +``db.numbers.find(<query>)`` method, the following results are |
| 272 | +returned: |
| 273 | + |
| 274 | +.. list-table:: |
| 275 | + :header-rows: 1 |
| 276 | + :widths: 30 70 |
| 277 | + |
| 278 | + * - Query |
| 279 | + - Results |
| 280 | + |
| 281 | + * - **{ "val": 9.99 }** |
| 282 | + - **{ "_id": 2, "val": 9.99, "description": "Double" }** |
| 283 | + |
| 284 | + * - **{ "val": NumberDecimal( "9.99" ) }** |
| 285 | + - **{ "_id": 1, "val": NumberDecimal( "9.99" ), "description": "Decimal" }** |
| 286 | + |
| 287 | + * - **{ val: 10 }** |
| 288 | + - | **{ "_id": 3, "val": 10, "description": "Double" }** |
| 289 | + | **{ "_id": 4, "val": NumberLong(10), "description": "Long" }** |
| 290 | + | **{ "_id": 5, "val": NumberDecimal( "10.0" ), "description": "Decimal" }** |
| 291 | + |
| 292 | + * - **{ val: NumberDecimal( "10" ) }** |
| 293 | + - | **{ "_id": 3, "val": 10, "description": "Double" }** |
| 294 | + | **{ "_id": 4, "val": NumberLong(10), "description": "Long" }** |
| 295 | + | **{ "_id": 5, "val": NumberDecimal( "10.0" ), "description": "Decimal" }** |
| 296 | + |
| 297 | + |
| 298 | +The first query, ``{ "val": 9.99 }``, implicitly searches for the |
| 299 | +``double`` representation of ``9.99`` which is not equal to the |
| 300 | +``decimal`` representation of the value. |
| 301 | + |
| 302 | +The ``NumberDecimal()`` constructor is used to query for the document |
| 303 | +with the ``decimal`` representation of ``9.99``. Values of the |
| 304 | +``double`` type are excluded because they do not match the exact value |
| 305 | +of the ``decimal`` representation of ``9.99``. |
| 306 | + |
| 307 | +Matching values of all numeric types are returned when querying for |
| 308 | +whole numbers. For example, querying for a ``double`` representation of |
| 309 | +``10`` will include a ``decimal`` representation of ``10.0`` in the |
| 310 | +results and vice versa. |
| 311 | + |
| 312 | +Checking for ``decimal`` Type |
| 313 | +````````````````````````````` |
| 314 | + |
| 315 | +To test for ``decimal`` type, use the :query:`$type` operator with the |
| 316 | +string alias ``"decimal"`` or ``19``, the numeric code for the |
| 317 | +``decimal`` type. |
| 318 | + |
| 319 | +.. code-block:: javascript |
| 320 | + |
| 321 | + db.inventory.find( { price: { $type: "decimal" } } ) |
| 322 | + |
| 323 | +.. _check-types-in-shell: |
| 324 | + |
| 325 | +Check Types in the ``mongo`` Shell |
| 326 | +---------------------------------- |
| 327 | + |
| 328 | +To determine the type of fields, ``mongosh`` provides the ``instanceof`` |
| 329 | +and ``typeof`` operators. |
| 330 | + |
| 331 | + |
| 332 | +``instanceof`` |
| 333 | +~~~~~~~~~~~~~~ |
| 334 | + |
| 335 | +``instanceof`` returns a boolean to test if a value is an instance of |
| 336 | +some type. |
| 337 | + |
| 338 | +For example, the following operation tests whether the ``_id`` field is |
| 339 | +an instance of type ``ObjectId``: |
| 340 | + |
| 341 | +.. code-block:: javascript |
| 342 | + |
| 343 | + mydoc._id instanceof ObjectId |
| 344 | + |
| 345 | +The operation returns ``true``. |
| 346 | + |
| 347 | +``typeof`` |
| 348 | +~~~~~~~~~~ |
| 349 | + |
| 350 | +``typeof`` returns the type of a field. |
| 351 | + |
| 352 | +For example, the following operation returns the type of the ``_id`` |
| 353 | +field: |
| 354 | + |
| 355 | +.. code-block:: javascript |
| 356 | + |
| 357 | + typeof mydoc._id |
| 358 | + |
| 359 | +In this case ``typeof`` will return the more generic ``object`` type |
| 360 | +rather than ``ObjectId`` type. |
0 commit comments