How to store unsigned long long (uint64_t) values in a MongoDB document? How to store unsigned long long (uint64_t) values in a MongoDB document? mongodb mongodb

How to store unsigned long long (uint64_t) values in a MongoDB document?


Because MongoDB/BSON only supports signed 64-bit integers, you'll need to cast your 64-bit unsigned values to/from long long when interacting with the database.

All 64-bits are still used so you won't lose any of the available unsigned range, it will just show as a negative value in the database for those values that use the most significant bit.


With all due respect, IMHO the best thing you can do is to reconsider your decision not to use string for the ID since:

  1. probably it is much more efficient than you ever though due to the amount of overhead of bson internals when storing 64B double which usually is 3 additional fields and field names (see 'floatApprox','top', bottom' here , here and here)

  2. it is already provided as id_str by twitter API you are using for good reason

  3. you are going to have real trouble casting those numbers in JS ifyou ever happen to use mongodb map reduce