Nic Cottrell
Nic Cottrell

Reputation: 9665

Is it OK to mixed NumberLong and normal integers in the same field in MongoDB?

I have been using Morphia to persistent objects from Java. I have also been running some batch processes from the console. I just realised that some values are now stored as NumberLong and number as plain Javascript numbers.

I have an index on this field. Everything seems to be ok - if I query: {f: 100} from the console it still returns the object even if it actually contains {f: NumberLong(100)}

Is this true of all the drivers? It is best practice to avoid NumberLong is I can fit the value inside 32-bit? Will I save a lot of data and index space if I convert all NumberLongs to basic numbers?

Upvotes: 1

Views: 644

Answers (1)

Tad Marshall
Tad Marshall

Reputation: 1363

Plain JavaScript numbers are 64-bit floats (double) and take up 8 bytes so space savings would not be a reason to do this. There probably is value in being consistent in your values, and there are tradeoffs between the different numeric types. If you want 32-bit integers, there is a type for that (NumberInt) in the shell. The drivers generally try to map things into the language of the driver but features match between drivers to the extent possible.

Upvotes: 1

Related Questions