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

Wrong values with NumberLong for numbers > Number.MAX_SAFE_INTEGER

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 0.2.2
    • Fix Version/s: 0.3.1
    • Component/s: Shell, Shell API
    • Labels:
      None
    • # Replies:
      0
    • Last comment by Customer:
      true
    • Documentation Changes:
      Needed
    • Documentation Changes Summary:
      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.  
    • Sprint:
      Iteration Lime

      Description

      `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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              maurizio.casimirri Maurizio Casimirri
              Participants:
              Last commenter:
              Maurizio Casimirri Maurizio Casimirri
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since reply:
                34 weeks, 6 days ago