Home > Software engineering >  How to force Int32 - Int64 instead of Double with MongoDB Node JS?
How to force Int32 - Int64 instead of Double with MongoDB Node JS?

Time:08-20

I would like to save int32 or int64 values from my Atlas Trigger (NodeJS code), but when i save any value it saves it as a Double.

user_collection.updateOne({"_id": "anyID"}, {$inc: {"score": 2}});

With the above line, score is a type of Double in the database.
I would like to have it as int64.

How can i force the use of int64 (or int32) in that case please ?

CodePudding user response:

You can just use :

user_collection.updateOne({"_id": "anyID"}, {$inc: {"score": parseInt(2, 10)}});

And it will save an Int32 in your database.

CodePudding user response:

From documentation:

Int32

If a number can be converted to a 32-bit integer, mongosh will store it as Int32. If not, mongosh defaults to storing the number as a Double. Numerical values that are stored as Int32 in mongosh would have been stored by default as Double in the mongo shell.

The Int32() constructor can be used to explicitly specify 32-bit integers.

Warning

Default Int32 and Double types may be stored inconsistently if you connect to the same collection using both mongosh and the legacy mongo shell.

Long

The Long() constructor can be used to explicitly specify a 64-bit integer.

So it is really confusing. You may prefer the Long methods if you like to modify the value.

Note, mongosh is also a Node.js terminal.

  • Related