skip to Main Content

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 ?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. You can just use :

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

    And it will save an Int32 in your database.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search