Navigation
This version of the manual is no longer supported.

Data Types in the mongo Shell

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 an ISODate object when used with the new operator.
  • ISODate() constructor which returns an ISODate object when used with or without the new operator.

Consider the following examples:

  • 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.

  • To get the date as an ISODate object, instantiate a new instance using the Date() constructor with the new operator, as in the following example:

    var myDateObject = new Date();
    
    • To print the value of the variable, type the variable name in the shell, as in the following:

      myDateObject
      

      The result is the value of myDateObject:

      ISODate("2012-12-19T06:01:17.171Z")
      
    • To verify the type, use the typeof operator, as in the following:

      typeof myDateObject
      

      The operation returns object.

  • To get the date as an ISODate object, instantiate a new instance using the ISODate() constructor without the new operator, as in the following example:

    var myDateObject2 = 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:

      myDateObject2
      

      The result is the value of myDateObject2:

      ISODate("2012-12-19T06:15:33.035Z")
      
    • To verify the type, use the typeof operator, as in the following:

      typeof myDateObject2
      

      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:

new ObjectId

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:

NumberLong("2090845886852")

The following examples use the NumberLong() class to write to the collection:

db.collection.insert( { _id: 10, calc: NumberLong("2090845886852") } )
db.collection.update( { _id: 10 },
                      { $set:  { calc: NumberLong("2555555000000") } } )
db.collection.update( { _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:

  1. Use $inc to increment the calc field by 5, which the mongo shell treats as a float:

    db.collection.update( { _id: 10 },
                          { $inc: { calc: 5 } } )
    
  2. Retrieve the updated document:

    db.collection.findOne( { _id: 10 } )
    

    In the updated document, the calc field contains a floating point value:

    { "_id" : 10, "calc" : 2555555000010 }
    

NumberInt

By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides the NumberInt() constructor to explicitly specify 32-bit integers.