Unexpected result when casting oid to date

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 8.2.11
    • Component/s: None
    • Query Execution
    • ALL
    • Hide
      const dbName = 'test';
      const testDb = db.getSiblingDB(dbName);
      const coll = testDb.oid_future;
      
      function cleanup() {
      try \{ db.getMongo().getDB(dbName).dropDatabase(); } catch (e) {}
      }
      
      cleanup();
      printjson(coll.insert(\{_id: 1, oid: ObjectId('8071be82122d3312074f0300')}));
      
      const doc = coll.aggregate([
      {$project: {
      _id:0,
      iso: \{$dateToString: {date: {$toDate:'$oid'}, format:'%Y-%m-%dT%H:%M:%S.%LZ', timezone:'UTC'}},
      ts: \{$toLong: {$toDate:'$oid'}}
      }}
      ]).toArray()[0];
      
      printjson({
      expectedIso:'2038-04-15T09:53:06.000Z',
      observed:doc,
      matchesExpected:doc.iso ==='2038-04-15T09:53:06.000Z'
      });
      
      cleanup();
      
      Show
      const dbName = 'test' ; const testDb = db.getSiblingDB(dbName); const coll = testDb.oid_future; function cleanup() { try \{ db.getMongo().getDB(dbName).dropDatabase(); } catch (e) {} } cleanup(); printjson(coll. insert (\{_id: 1, oid: ObjectId( '8071be82122d3312074f0300' )})); const doc = coll. aggregate ([ {$project: { _id:0, iso: \{$dateToString: { date : {$toDate: '$oid' }, format: '%Y-% m -%dT%H:% M :%S.%LZ' , timezone: 'UTC' }}, ts: \{$toLong: {$toDate: '$oid' }} }} ]).toArray()[0]; printjson({ expectedIso: '2038-04-15T09:53:06.000Z' , observed:doc, matchesExpected:doc.iso === '2038-04-15T09:53:06.000Z' }); cleanup();
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Hi,

       

      I found that when casting the oid to data, MongoDB sometimes returns an incorrect result.

       

      This is an example:

       

      const dbName = 'test';
      const testDb = db.getSiblingDB(dbName);
      const coll = testDb.oid_future;
      
      function cleanup() {
      try \{ db.getMongo().getDB(dbName).dropDatabase(); } catch (e) {}
      }
      
      cleanup();
      printjson(coll.insert(\{_id: 1, oid: ObjectId('8071be82122d3312074f0300')}));
      
      const doc = coll.aggregate([
      {$project: {
      _id:0,
      iso: \{$dateToString: {date: {$toDate:'$oid'}, format:'%Y-%m-%dT%H:%M:%S.%LZ', timezone:'UTC'}},
      ts: \{$toLong: {$toDate:'$oid'}}
      }}
      ]).toArray()[0];
      
      printjson({
      expectedIso:'2038-04-15T09:53:06.000Z',
      observed:doc,
      matchesExpected:doc.iso ==='2038-04-15T09:53:06.000Z'
      });
      
      cleanup();
      

       

      This is the outputs:

      
      {
        expectedIso: '2038-04-15T09:53:06.000Z',
        observed: {
          iso: '1902-03-10T03:24:50.000Z',
          ts: Long('-2140029310000')
        },
        matchesExpected: false
      }
      
      

       

      When casting oid to date, the first byte are used, which is `80 71 be 82` in this example. The corresponding Unix timestamp is 2154937986, so the UTC time should be 2038-04-15T09:53:06.000Z.

            Assignee:
            Unassigned
            Reporter:
            Chi Zhang (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: