5

The number is larger than 9223372036854775807 - too big for NumberLong, which is mongo's native 64-bit long type. What's the best way to do this/the best field type?

Is it possible to preserve any of the querying functionality of a smaller integer (such as {$lt})?

The big numbers are being generated by bignumber.js, and I'm using mongoose to interact with mongoDb.

Sawyer
  • 221
  • 3
  • 6
  • As a string ? String can be longer and store bigger numbers. But you have to converti to and from string. – Kulvar Mar 31 '17 at 00:23
  • 1
    Yeah I'm considering that - but I'd love to be able to run $max and $lt queries. Also considering storing two fields: the whole number quotient of `x/2^63`, and `x%2^63` - then I could query for max by combining params on both fields – Sawyer Mar 31 '17 at 15:45
  • thats exactly how people do it in SQL databases http://stackoverflow.com/questions/420680/how-to-store-ipv6-compatible-address-in-a-relational-database – bauman.space Mar 31 '17 at 17:57

1 Answers1

1

I'm afraid that the only viable/safe option would be to store such big numbers as a string and serialize it back and forth between the application and MongoDB when read/written. However, you will loose the ability to use MongoDB built-in functions that work with numbers (you can still cast the values to numbers, but it won't be safe anymore).

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202