DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • MongoDB Basics in 5 Minutes
  • Tracking Changes in MongoDB With Scala and Akka
  • Loading XML into MongoDB
  • Spring Boot GoT: Game of Trace!

Trending

  • How to Practice TDD With Kotlin
  • Java Virtual Threads and Scaling
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • A Modern Stack for Building Scalable Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. Long Numbers Are Truncated in MongoDB Shell

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.

By 
Ricci Gian Maria user avatar
Ricci Gian Maria
·
Jun. 06, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
11.2K Views

Join the DZone community and get the full member experience.

Join For Free

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:

mongodb

shell MongoDB

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

Opinions expressed by DZone contributors are their own.

Related

  • MongoDB Basics in 5 Minutes
  • Tracking Changes in MongoDB With Scala and Akka
  • Loading XML into MongoDB
  • Spring Boot GoT: Game of Trace!

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: