- Administration >
- Administration Tutorials >
- MongoDB Scripting >
- Data Types in the
mongoShell
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.
Types¶
Date¶
The mongo shell provides various methods to return the date,
either as a string or as a Date object:
Date()method which returns the current date as a string.new Date()constructor which returns aDateobject using theISODate()wrapper.ISODate()constructor which returns aDateobject using theISODate()wrapper.
Return Date as a String¶
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.
Return Date¶
The mongo shell wrap objects of Date type with the
ISODate helper; however, the objects remain of type Date.
The following example uses both the new Date() constructor and the
ISODate() constructor to return Date objects.
You can use the new operator with the ISODate() 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 Date value of myDate wrapped in the
ISODate() helper:
To verify the type, use the instanceof operator, as in the
following:
The operation returns true for both.
ObjectId¶
The mongo shell provides the ObjectId() wrapper class
around the ObjectId data type. 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() wrapper to handle 64-bit integers.
The NumberLong() wrapper accepts the long as a string:
The following examples use the NumberLong() wrapper 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:
Check Types in the mongo Shell¶
To determine the type of fields, the mongo shell provides
the instanceof and typeof operators.
instanceof¶
instanceof returns a boolean to test if a value is an instance of
some type.
For example, the following operation tests whether the _id field is
an instance of type ObjectId:
The operation returns true.
typeof¶
typeof returns the type of a field.
For example, the following operation returns the type of the _id
field:
In this case typeof will return the more generic object type
rather than ObjectId type.