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

ObjectId getTimestamp helper throws an error

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • No version
    • Affects Version/s: None
    • Component/s: sh
    • None
    • Environment:
      OS:
      node.js / npm versions:
      Additional info:
    • 2
    • Not Needed

      Using mongosh 1.6.0 the following code will fail:

      (new ObjectId()).getTimestamp()
      // TypeError: Cannot read properties of undefined (reading 'slice')
      

      The above does work with the legacy shell and appears to be due to the implementation of ObjectId.prototype.getTimestamp calling ObjectId.prototype.valueOf, which returns undefined (instead of a string).

      This can be fixed by changing ObjectId.prototype.getTimestamp to:

      ObjectId.prototype.getTimestamp = function() {  
        if (this.toHexString) return new Date(parseInt(this.toHexString().slice(0, 8), 16) * 1000);  
        return new Date(parseInt(this.toString('hex').slice(0, 8), 16) * 1000);
      };
      

            Assignee:
            gaurab.aryal@mongodb.com Gaurab Aryal
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: