On this page
Data Types in the mongo Shell
On this page
MongoDB BSON provides 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 the Extended JSON reference 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.
Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).
Not all database operations and drivers support the full 64-bit range. You may safely work with dates with years within the inclusive range 0 through 9999.
Return Date as a String
To return the date as a string, use the Date() method, as in the following example:
var myDateString = Date();
       To print the value of the variable, type the variable name in the shell, as in the following:
myDateString
       The result is the value of myDateString:
Wed Dec 19 2012 01:03:25 GMT-0500 (EST)
       To verify the type, use the typeof operator, as in the following:
typeof myDateString
       The operation returns string.
Return Date
     The mongo shell wraps 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.
var myDate = new Date();
var myDateInitUsingISODateWrapper = ISODate();
       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:
myDate
       The result is the Date value of myDate wrapped in the ISODate() helper:
ISODate("2012-12-19T06:01:17.171Z")
       To verify the type, use the instanceof operator, as in the following:
myDate instanceof Date
myDateInitUsingISODateWrapper instanceof Date
       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:
new ObjectId
      See
NumberLong
The mongo shell treats all numbers as floating-point values by default. The mongo shell provides the NumberLong() wrapper to handle 64-bit integers.
The NumberLong() wrapper accepts the long as a string:
NumberLong("2090845886852")
      The following examples use the NumberLong() wrapper to write to the collection:
db.collection.insertOne( { _id: 10, calc: NumberLong("2090845886852") } )
db.collection.updateOne( { _id: 10 },
                      { $set:  { calc: NumberLong("2555555000000") } } )
db.collection.updateOne( { _id: 10 },
                      { $inc: { calc: NumberLong(5) } } )
      Retrieve the document to verify:
db.collection.findOne( { _id: 10 } )
      In the returned document, the calc field contains a NumberLong object:
{ "_id" : 10, "calc" : NumberLong("2555555000005") }
      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:
Use
$incto increment thecalcfield by5, which themongoshell treats as a float:db.collection.updateOne( { _id: 10 }, { $inc: { calc: 5 } } )Retrieve the updated document:
db.collection.findOne( { _id: 10 } )In the updated document, the
calcfield contains a floating point value:{ "_id" : 10, "calc" : 2555555000010 }
NumberInt
The mongo shell treats all numbers as floating-point values by default. The mongo shell provides the NumberInt() constructor to explicitly specify 32-bit integers.
NumberDecimal
New in version 3.4.
The mongo shell treats all numbers as 64-bit floating-point double values by default. The mongo shell provides the NumberDecimal() constructor to explicitly specify 128-bit decimal-based floating-point values capable of emulating decimal rounding with exact precision. This functionality is intended for applications that handle monetary data, such as financial, tax, and scientific computations.
The decimal BSON type uses the IEEE 754 decimal128 floating-point numbering format which supports 34 decimal digits (i.e. significant digits) and an exponent range of -6143 to +6144.
The NumberDecimal() constructor accepts the decimal value as a string:
NumberDecimal("1000.55")
      The value is stored in the database as follows:
NumberDecimal("1000.55")
      The NumberDecimal() constructor also accepts