- Using the
mongo
Shell > - Data Types in the
mongo
Shell
Data Types in the mongo
Shell¶
On this page
MongoDB BSON provide support for additional data types than
JSON. Drivers provide native
support for these data types in host languages and the
mongo
shell also provides several helper classes to support
the use of these data types in the mongo
JavaScript
shell. See MongoDB Extended JSON for additional
information.
Date¶
The mongo
shell provides various options to return the date,
either as a string or as an object:
Date()
method which returns the current date as a string.Date()
constructor which returns anISODate
object when used with thenew
operator.ISODate()
constructor which returns anISODate
object when used with or without thenew
operator.
Consider the following examples:
To return the date as a string, use the
Date()
method, as in the following example:To print the value of the variable, type the variable name in the shell, as in the following:
The result is the value of
myDateString
:To verify the type, use the
typeof
operator, as in the following:The operation returns
string
.
To get the date as an
ISODate
object, instantiate a new instance using theDate()
constructor with thenew
operator, as in the following example:To print the value of the variable, type the variable name in the shell, as in the following:
The result is the value of
myDateObject
:To verify the type, use the
typeof
operator, as in the following:The operation returns
object
.
To get the date as an
ISODate
object, instantiate a new instance using theISODate()
constructor without thenew
operator, as in the following example:You can use the
new
operator with theISODate()
constructor as well.To print the value of the variable, type the variable name in the shell, as in the following:
The result is the value of
myDateObject2
:To verify the type, use the
typeof
operator, as in the following:The operation returns
object
.
ObjectId¶
The mongo
shell provides the ObjectId()
wrapper class
around ObjectId data types. To generate a new ObjectId, use
the following operation in the mongo
shell:
See
ObjectId for full documentation of ObjectIds in MongoDB.
NumberLong¶
By default, the mongo
shell treats all numbers as
floating-point values. The mongo
shell provides the
NumberLong()
class to handle 64-bit integers.
The NumberLong()
constructor accepts the long as a string:
The following examples use the NumberLong()
class to write to the
collection:
Retrieve the document to verify:
In the returned document, the calc
field contains a
NumberLong
object:
If you use the $inc
to increment the value of a field that
contains a NumberLong
object by a float, the data type changes
to a floating point value, as in the following example: