Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 62 additions & 18 deletions source/core/shell-types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,66 @@ the following operation in the :program:`mongo` shell:
.. see:: :doc:`/core/object-id` for full documentation of ObjectIds in
MongoDB.

.. _shell-type-long:

.. wiki content -- I don't think the following applies anymore

Numbers
By default, the shell treats all numbers as floating-point values.
You have the option to work with 64-bit integers by using a class
built into the shell called NumberLong() If you have long/integer
BSON data from the database you may see something like this:
{"count" : NumberLong("575175")}
Setting/incrementing any number from javascript will (most likely)
change the data type to a floating point value.
Here is an example of creating a document with a long field:
doc = { field: new NumberLong("123212313")}
Note that prior to 1.6 long numbers might be displayed like this:
"bytes" : {
"floatApprox" : 5284376243087482000,
"top" : 1230364721,
"bottom" : 4240317554
}
NumberLong
----------

By default, the :program:`mongo` shell treats all numbers as
floating-point values. The :program:`mongo` shell provides the
``NumberLong()`` class to handle 64-bit integers.

The ``NumberLong()`` constructor accepts the long as a string:

.. code-block:: javascript

NumberLong("2090845886852")

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

.. code-block:: javascript

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:

.. code-block:: javascript

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

In the returned document, the ``calc`` field contains a
``NumberLong`` object:

.. code-block:: sh

{ "_id" : 10, "calc" : NumberLong("2555555000005") }

If you :operator:`increment <$inc>` the field that contains a
``NumberLong`` object by a **float**, the data type changes to a
floating point value, as in the following example:

#. :operator:`Increment <$inc>` the ``calc`` field by ``5`` which the
:program:`mongo` shell treats as a float:

.. code-block:: javascript

db.collection.update( { _id: 10 },
{ $inc: { calc: 5 } } )

#. Retrieve the updated document:

.. code-block:: javascript

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

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

.. code-block:: sh

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