Use NumberLong to store 64 bit integers.
Sort Twitter tweet id string as a number on MongoDB
-
26-06-2022 - |
Question
I want to store tweets on MongoDB and then find/order them by id, Twitter API provide two IDs types for a tweet, one as a number type and the other a string type because JavaScript (mongo/node) can only have 53 bit integers and Twitter IDs use more than that.
If I store the IDs as numbers the ID get rounded and lost BUT I can sort and find the IDs with a criteria as "greater than" and "less than".
If I sotre the IDs as strings the ID number get stored as it should BUT I can't find (select) ids with a criteria of "less than" or "greater than" and sorting is a mess because it is sorting the "field" as a string, so "10" shows before "3" because it has "1" at the beginning.
I need to store this IDs and I want to be able to select them with a criteria as "less/greater/equal/than" and sort them by that "field" as numbers...
How can I achieve this?
Thanks a lot!
Solution
OTHER TIPS
It's always possible to prepend number with zeroes and store it as a strings, like
000000010
000000003
so you can store long IDs and use you greater/less/equal conditions
If you are using PHP, use:
ini_set('mongo.native_long', 1);