Stack Exchange User
Stack Exchange User

Reputation: 778

MongoDB and big numerical document IDs

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?

Upvotes: 2

Views: 348

Answers (2)

Neil Lunn
Neil Lunn

Reputation: 151112

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.

Upvotes: 0

Chris Heald
Chris Heald

Reputation: 62648

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"}

Upvotes: 2

Related Questions