简体   繁体   中英

How to force Int32 - Int64 instead of Double with MongoDB Node JS?

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?

You can just use:

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

And it will save an Int32 in your database.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM