0

I am using SailsJS with MongoDB and model is having type number as below:

attributes: {
    mobile_no: {
      type: 'number'
    }
} 

So when I insert with API it is working perfectly up to 10 numeric character length.

For more than 10+ numeric character length my document's field is automatically gets converted to float and adds decimal points something like 12345678902.0 but I don't want that because mobile_no field can have more than 10 characters.

Mysterious Coder
  • 148
  • 2
  • 4
  • 20
  • 2
    Mobile "numbers" (and other communication "numbers") should be character fields - if you ever have to cater for leading "0", you're busted. – Filburt Oct 22 '19 at 13:04
  • 2
    You should normaly store mobile numbers as `strings` – Minato Oct 22 '19 at 13:04
  • 3
    Phone numbers, credit card numbers, etc. are logically strings. – Ry- Oct 22 '19 at 13:04
  • 1
    The easiest way to solve this problem is, you should write a migration script and convert all the mobile_no data to `string` format. – Minato Oct 22 '19 at 13:06
  • Thank you guys for clearing my doubt – Mysterious Coder Oct 22 '19 at 13:06
  • but MongoDB is displaying int32, int64 in the field type, Why I can't do int64 ? – Mysterious Coder Oct 22 '19 at 13:09
  • 1
    JavaScript’s only number type was double-precision floating point for a long time, with some operations on 32-bit integers. `BigInt` exists now, but MongoDB (and MongoDB drivers for Node) predates it, so any int64s you get from Mongo are losing precision. – Ry- Oct 23 '19 at 15:46

0 Answers0