Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-341

Wrong values with NumberLong for numbers > Number.MAX_SAFE_INTEGER

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 0.3.1
    • Affects Version/s: 0.2.2
    • Component/s: Embedded Shell, Shell API
    • Labels:
      None
    • Needed
    • Hide

      From this version we require NumberLong and NumberDecimal to be constructed from a string. This is a breaking change from the old shell behavior.

      Now: NumberLong("123") and NumberDecimal("123.1") would work, whereas: NumberLong(123) and NumberDecimal(123.1) would throw an exception.

      Show
      From this version we require NumberLong and NumberDecimal to be constructed from a string. This is a breaking change from the old shell behavior. Now: NumberLong("123") and NumberDecimal("123.1") would work, whereas: NumberLong(123) and NumberDecimal(123.1) would throw an exception.
    • Iteration Lime

      `NumberLong` is created incorrectly with unsafe integers (> Number.MAX_SAFE_INTEGER)

      The same happens when retrieving a NumberLong which is an unsafe integer, even if was correctly inserted in the database.

      The old shell correctly handles it:

      New shell:

      > db.coll3.insertOne({x: NumberLong("345678654321234561")})
      { acknowledged: 1, insertedId: ObjectId("5f564a3c4f53d376a739e656") }
      > db.coll3.find()
      [
        {
          _id: ObjectId("5f564a3c4f53d376a739e656"),
          x: NumberLong(345678654321234560)
        }
      ] 
      

      Old shell:

      > db.coll5.insertOne({int64: NumberLong("345678654321234561")})
      {
              "acknowledged" : true,
              "insertedId" : ObjectId("5f564a9284f79fe8de9e7e40")
      }
      > db.coll5.find()
      { "_id" : ObjectId("5f564a9284f79fe8de9e7e40"), "int64" : NumberLong("345678654321234561") }
      

      NOTE: looks like is enough to use fromString (converting it before) instead of fromNumber here: https://github.com/mongodb-js/mongosh/blob/e4fb6ea6eae5f5d196038c68301af5bffea57070/packages/shell-api/src/shell-bson.ts#L122

            Assignee:
            Unassigned Unassigned
            Reporter:
            maurizio.casimirri@mongodb.com Maurizio Casimirri
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: