From 8657ef625befddf387017434455d13e6029da141 Mon Sep 17 00:00:00 2001 From: kay Date: Tue, 8 Jan 2013 11:05:20 -0500 Subject: [PATCH 1/3] DOCS-948 NumberLong in shell --- source/core/shell-types.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/source/core/shell-types.txt b/source/core/shell-types.txt index 7d4b05c70a6..37471c74a92 100644 --- a/source/core/shell-types.txt +++ b/source/core/shell-types.txt @@ -136,6 +136,28 @@ the following operation in the :program:`mongo` shell: .. see:: :doc:`/core/object-id` for full documentation of ObjectIds in MongoDB. +.. _shell-type-long: + +NumberLong +---------- + +The :program:`mongo` shell provides the ``NumberLong()`` class to handle +numbers of type long. + +The ``NumberLong()`` accepts the long as a string: + +.. code-block:: javascript + + NumberLong("20908458868526654") + +The following examples use the ``NumberLong()`` class to write to the +collection: + +.. code-block:: javascript + + db.collection.insert( { _id: 10, calc: NumberLong("20908458868526654") } ) + db.collection.update( { _id: 10 }, + { $set: { calc: NumberLong("209084588685266550") } } ) .. wiki content -- I don't think the following applies anymore From 43ddc515b0ed8ab0c3cc2adc7244f612750f089a Mon Sep 17 00:00:00 2001 From: kay Date: Wed, 9 Jan 2013 19:44:23 -0500 Subject: [PATCH 2/3] DOCS-948 incorporate Kristina's feedback --- source/core/shell-types.txt | 48 +++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/source/core/shell-types.txt b/source/core/shell-types.txt index 37471c74a92..8a75f5b5c34 100644 --- a/source/core/shell-types.txt +++ b/source/core/shell-types.txt @@ -141,10 +141,11 @@ the following operation in the :program:`mongo` shell: NumberLong ---------- -The :program:`mongo` shell provides the ``NumberLong()`` class to handle -numbers of type long. +By default, the :program:`mongo` shell treats all numbers as +floating-point values. The :program:`mongo` shell provides the +``NumberLong()`` class to handle numbers of 64-bit integers. -The ``NumberLong()`` accepts the long as a string: +The ``NumberLong()`` constructor accepts the long as a string: .. code-block:: javascript @@ -159,21 +160,26 @@ collection: db.collection.update( { _id: 10 }, { $set: { calc: NumberLong("209084588685266550") } } ) -.. 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 - } +If you :operator:`increment <$inc>` the field that contains a +``NumberLong`` object, the data type changes to a floating point value, +as in the following example: + +#. :operator:`Increment <$inc>` the ``calc`` field: + + .. code-block:: javascript + + db.collection.update( { _id: 10 }, + { $inc: { calc: 5 } } ) + +#. Select 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" : 209084588685266560 } From 6dfec430198c6b39c2c45e7ec16c52ea08b5cd6a Mon Sep 17 00:00:00 2001 From: kay Date: Thu, 10 Jan 2013 12:21:36 -0500 Subject: [PATCH 3/3] DOCS-948 incorporate kristina's feedback --- source/core/shell-types.txt | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/source/core/shell-types.txt b/source/core/shell-types.txt index 8a75f5b5c34..9414786fa9c 100644 --- a/source/core/shell-types.txt +++ b/source/core/shell-types.txt @@ -143,35 +143,51 @@ NumberLong By default, the :program:`mongo` shell treats all numbers as floating-point values. The :program:`mongo` shell provides the -``NumberLong()`` class to handle numbers of 64-bit integers. +``NumberLong()`` class to handle 64-bit integers. The ``NumberLong()`` constructor accepts the long as a string: .. code-block:: javascript - NumberLong("20908458868526654") + NumberLong("2090845886852") The following examples use the ``NumberLong()`` class to write to the collection: .. code-block:: javascript - db.collection.insert( { _id: 10, calc: NumberLong("20908458868526654") } ) + db.collection.insert( { _id: 10, calc: NumberLong("2090845886852") } ) db.collection.update( { _id: 10 }, - { $set: { calc: NumberLong("209084588685266550") } } ) + { $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, the data type changes to a floating point value, -as in the following example: +``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: +#. :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 } } ) -#. Select the updated document: +#. Retrieve the updated document: .. code-block:: javascript @@ -182,4 +198,4 @@ as in the following example: .. code-block:: sh - { "_id" : 10, "calc" : 209084588685266560 } + { "_id" : 10, "calc" : 2555555000010 }