Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

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

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

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:

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:
mongo ,extension ,double

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}