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

三世轮回 提交于 2020-01-02 04:10:31

问题


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*


回答1:


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")


来源:https://stackoverflow.com/questions/42097779/how-can-mongodb-handle-objectid-timestamp-beyond-tue-19-jan-2038

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!