[SERVER-50444] mongo shell serializes large NumberLongs incorrectly Created: 21/Aug/20  Updated: 27/Oct/23  Resolved: 21/Aug/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Arun Banala Assignee: Backlog - Query Team (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query
Operating System: ALL
Steps To Reproduce:

Steps to reproduce issue #1:

> db.c.insert({x: NumberLong(9223372036854774800)})
WriteResult({ "nInserted" : 1 })
> db.c.find()
{ "_id" : ObjectId("5f3f93740f6105e040a46651"), "x" : NumberLong("9223372036854774784") }

Steps to reproduce issue #2:

> db.c.insert({x: NumberLong(9223372036854775600)}) // This number should fit in BSON NumberLong
Error: number passed to NumberLong must be representable as an int64_t :
@(shell):1:17

Participants:

 Description   

I've noticed couple of issues with mongo shell

1. mongo shell is serializing large NumberLong values (which are valid) incorrectly, and send wrong value to the server.
2. We are enforcing a limit on numbers that could fit into BSON NumberLong data type.

Both these issues seem to be because we are converting the number into double first and then interpreting it as 'int64_t'



 Comments   
Comment by Arun Banala [ 21/Aug/20 ]

Ah, that makes sense. Thanks for providing the details. I'm going to close this ticket.

Comment by Bruce Lucas (Inactive) [ 21/Aug/20 ]

I think this is working as designed - the expression NumberLong(x) means construct a NumberLong object from the value x, and in this case x is a Javascript number, which is implemented by the Javascript interpreter as a double.

To construct the NumberLong represented by the string of digits 9223372036854774800 you can pass a string to NumberLong:

> db.c.insert({x: NumberLong("9223372036854774800")})
WriteResult({ "nInserted" : 1 })
> db.c.find()
{ "_id" : ObjectId("5f400f9dc47d8e7a36be1622"), "x" : NumberLong("9223372036854774800") }

Generated at Thu Feb 08 05:22:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.