Search code examples
mongodbuuid

GUID in MongoDB


I understand there is a limit to the number of unique id's created automatically. I think it is 16 million if I remember correctly. Can this be extended and or can one user GUID's as well to provide more uniqueness if the number of unique entities expands?

How many unique items can I, therefore, put into a collection, if I use the default object _id right out of the box. I read there are 12 bytes, but depending on how these are used means that there could be different numbers of unique documents. How can I compute how many unique documents I can store?


Solution

  • The theoretical max would be around 7.9228162514e+28. Where did you get 16 million?

    In reality the ObjectID consists of

    • A 4-byte timestamp, representing the ObjectId's creation, measured in seconds since the Unix epoch.

    • A 5-byte random value generated once per process. This random value is unique to the machine and process.

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

    So the 3 byte counter alone is 16777216 unique IDs every second.

    A 5-byte value would be 1.0995116277e+12 possebilities. So the machine and process unique value alone is around 16 billion unique combinations.