2

Mongodb uses BSON format to store data on the disk. BSON defines different data types, including signed int64 for storing big integers.

Let's try to save document with big ID (887190505588098573), that fits in signed int64 range (its absolute value is less than 2^63)

> db.query.insert({_id: 887190505588098573, 'q': 'zzz'})
> db.query.find({_id: 887190505588098573})
{ "_id" : 887190505588098600, "q" : "zzz" }

Well, we got response with document ID that differs from the ID we requested.

What am I missing?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317

2 Answers2

2

Javascript can't handle a number that big - it only handles integers up to 2^53.

You can see this by putting 887190505588098573 into a JS console and you'll receive 887190505588098600 back.

Non-JS clients hand this just fine. For example, Ruby:

jruby-1.7.12 :013 > c["test"]["query"].insert({_id: 887190505588098574, q: 'zzz'})
 => 887190505588098574
jruby-1.7.12 :016 > c["test"]["query"].find({_id: 887190505588098574}).next()
 => {"_id"=>887190505588098574, "q"=>"zzz"}
Community
  • 1
  • 1
Chris Heald
  • 61,439
  • 10
  • 123
  • 137
  • Thanks! Good news then! I've just faced with some problems with storing big integers in mongodb via python and then I've found the issue that I've described in my question. Now I have enough info to work on this problem. – Stack Exchange User Jun 13 '14 at 01:54
0

There is the NumberLong type in MongoDB that does conform to a 64-Bit Integer (BSON type 18)

db.collection.insert({ "_id": new NumberLong(887190505588098573) })

So that matches on the $type

db.collection.find({ "_id": { "$type": 18 } })

Mileage may vary as to where you can use this though, as a browser client might get an extended JSON form of this but there are limitations on how it can be used without similarly wrapping in a class to handle it.

Working with most other languages should be fine as the driver will cast to the native type. So it really is down to your language implementation as to the practicality of this. But MongoDB itself will handle it.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317