Long Numbers Are Truncated in MongoDB Shell

DZone 's Guide to

Long Numbers Are Truncated in MongoDB Shell

MongoDB is notorious for it's quirks; in this short read, MVB Ricci Gian Maria explains how he solved this problem with the popular document store.

· Database Zone ·
Free Resource

Let’s try this simple code in a Mongo shell:

db.TestCollection.insert({"_id" : 1, "Value" : NumberLong(636002954392732556) })

What you expect is that Mongo inserted one record and then that record is returned. Actually, a record is inserted, but the return value can surprise you. Here is the output I got from RoboMongo:

 "_id" : 1.0,
 "Value" : NumberLong(636002954392732544)

Property “Value” has not the number you inserted, the number seems to be rounded and some precision is lost, even if it is a NumberLong and 636002954392732556 is a perfectly valid Int64 number. This behavior surprised me because I’m expecting rounding to happen only with double, not with an Int64.

Actually, a double precision floating point number that uses 64 bit for representation, is not capable of having the same precision of an Int64 number, because part of those 64 bits are used to store an exponent. If you try to represent a big number like 636002954392732556 in Double Floating Point precision some rounding is going to happen. If you are not convinced, try this online converter to convert 636002954392732556, here is the result:

In this image there is a screenshot of the online converter, that exactly demonstrate that the rounding happens due to conversion to floating point number

Figure 1: Floating point number rounding

This confirms that my problem was indeed caused by rounding because the number is somewhat converted to Floating Point format, even if I used NumberLong bson extension to specify that I want a long and not a Floating Point type.

The reason behind this is subtle. Let's try another example, just type NumberLong(636002954392732556) in a Mongo shell (I used RoboMongo), and verify the result.

calling NumberLong(636002954392732556) function returns a rounded number,

Figure 2: NumberLong gets rounded directly from the shell.

This unveils the error, the number is returned surrounded with quotes, and this suggests that quotes are the problem. In JavaScript, every number is a double, and if you write NumberLong(636002954392732556) JavaScript translates this to a call to the NumberLong function passing the number 636002954392732556 as an argument. Since every number in JavaScript is a double, the number 636002954392732556 gets rounded before it is passed to the NumberLong function.

If you surround the number with quotes, you are passing a string to NumberLong—in this scenario, rounding does not occur and the NumberLong function is perfectly capable of converting the string to a number.

* In Mongo shell, always use quotes when you create numbers with NumberLong.

Actually, this error only happens with really big numbers, but you need to be aware of this if you are creating a script that uses NumberLong.

Related Refcard:

double, extension, mongo

Published at DZone with permission of Ricci Gian Maria , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}