Vincent
Vincent

Reputation: 482

Why mongoose store my number as Double

I try to understand why when I save a model containing a customId as number (for example 61528564963650091), it keeps saving it as a Double type:

61528564963650091.0

If the number is small, it creates a Int32

To solve my problem, I tried to use Long type but it was worse...

Schema example:

{
  customId: {
    type: Number,
    required: true,
  }
}

If someone can light me up on this :)

Upvotes: 2

Views: 3727

Answers (1)

libik
libik

Reputation: 23049

Max value of 32 bit integer is ~ 4 290 000 000, as you can see, you really cannot fit ther the 61 528 564 963 650 091.

Its not about mongo, its just the limitation of 32 bits.

Double is usually 64 bit implementation (thats why it is called double=2x32), which means it allows much bigger number to be saved as the one you have provided.


If I look to the newest mongoose docs: http://mongoosejs.com/docs/schematypes.html it looks like it does not support "Long" (also have in mind that Javascript has only one type - number - which is 64 bit floating point)


But there is solution - you can use this module: https://www.npmjs.com/package/mongoose-long

Upvotes: 3

Related Questions