Collection ID length in MongoDB

前端 未结 2 1287
挽巷
挽巷 2021-02-05 09:18

i am new to mongodb and stack overflow.

I want to know why on mongodb collection ID is of 24 hex characters? what is importance of that?

2条回答
  •  无人共我
    2021-02-05 09:45

    Now mongoDB current version is 4.2. ObjectId size is still 12 bytes but consist of 3 parts.

    ObjectIds are small, likely unique, fast to generate, and ordered. ObjectId values are 12 bytes in length, consisting of:

    • a 4-byte timestamp value, representing the ObjectId’s creation, measured in seconds since the Unix epoch

    • a 5-byte random value

    • a 3-byte incrementing counter, initialized to a random value

    Create ObjectId and get timestamp from it

    > x = ObjectId()
    ObjectId("5fdedb7c25ab1352eef88f60")
    > x.getTimestamp()
    ISODate("2020-12-20T05:05:00Z")
    

    Reference

    Read MongoDB official doc

提交回复
热议问题