Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-34975

Server should treat ObjectId timestamp as unsigned

    • Query Optimization
    • ALL
    • v4.0
    • Hide

      On the shell, where it correctly uses the unsigned 32-bit integer value:

      > ObjectId("8071be82122d3312074f0300").getTimestamp()
      
      ISODate("2038-04-15T09:53:06Z")
      

      In aggregation, where it incorrectly uses the timestamp as a signed 32-bit integer:

      > db.ids.aggregate([ { $project: { 'd' : { $dateToString: { date: ObjectId("8071be82122d3312074f0300"), format: '%Y-%m-%d' } } } } ]);
      
      { "_id" : ObjectId("8a71be82122d3312074f0300"), "d" : "1902-03-10" }
      
      Show
      On the shell, where it correctly uses the unsigned 32-bit integer value: > ObjectId( "8071be82122d3312074f0300" ).getTimestamp() ISODate( "2038-04-15T09:53:06Z" ) In aggregation, where it incorrectly uses the timestamp as a signed 32-bit integer: > db.ids.aggregate([ { $project: { 'd' : { $dateToString: { date: ObjectId( "8071be82122d3312074f0300" ), format: '%Y-%m-%d' } } } } ]); { "_id" : ObjectId( "8a71be82122d3312074f0300" ), "d" : "1902-03-10" }
    • Query 2018-06-04, Query 2018-07-02

      The ObjectId type in the server treats the timestamp portion as a 32-bit signed integer. This should be treated as an unsigned integer instead, increasing the range of dates into the future, and bringing the server more in line with the drivers.

      As one example of what can go wrong, see below.

      When the $dateToString function is applied to an ObjectID's timestamp portion, the timestamp portion is interpreted as a 32-bit signed integer. This is inconsistent with the shell's ObjectId.getTimestamp() method, and the definition of the first 4 bytes of the https://docs.mongodb.com/manual/reference/method/ObjectId/: "a 4-byte value representing the seconds since the Unix epoch"

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            derick Derick Rethans
            Votes:
            0 Vote for this issue
            Watchers:
            13 Start watching this issue

              Created:
              Updated: