Basim Hennawi
Basim Hennawi

Reputation: 2711

How can mongodb handle ObjectId timestamp beyond Tue, 19 Jan 2038?

As per MongoDB official docs, it states that:

ObjectId values consists of 12-bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation, specifically:

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 3-byte machine identifier,
  • a 2-byte process id, and
  • a 3-byte counter, starting with a random value.

I'm just wondering what's gonna happen on Tue, 19 Jan 2038 03:14:08 GMT when the unix time will be equal to 2147483648 which doesn't fit the 4-byte timestamp in ObjectId *philosoraptor meme*

Upvotes: 11

Views: 1689

Answers (1)

Alex Blex
Alex Blex

Reputation: 37018

Unsigned 2,147,483,648 perfectly fits into 4 bytes. 4 bytes is enough to hold values up to 4,294,967,295, which is a unix epoch for Sunday, 7 February 2106 06:28:16 GMT.

If ObjectID's survive without changes till then, the timestamp part will start from 0, if you care:

> new Date();
ISODate("2106-02-08T12:41:20.450Z")

> db.t.insert({welcome:"from the future"});
WriteResult({ "nInserted" : 1 })

> db.t.find().pretty();
{
    "_id" : ObjectId("0001a7b306c389186a3a9323"),
    "welcome" : "from the future"
}

> db.t.find()[0]._id.getTimestamp();
ISODate("1970-01-02T06:07:47Z")

Upvotes: 7

Related Questions