Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which algorithm does MongoDB use for _id [duplicate]

Tags:

mongodb

Which algorithm does MongoDB use for each document _id?

I could not find any documentation about it. Is it some kind of uuid?

like image 925
wutzebaer Avatar asked Dec 11 '22 05:12

wutzebaer


1 Answers

ObjectId is the default type for "_id" and it use 12 bytes of storage,which gives them a string representation that is 24 hexadecimal digits: 2 digits for each byte. This is generated on the client side.If you create multiple new ObjectIds in rapid succession, you can see that only the last few digits change each time. In addition, a couple of digits in the middle of the ObjectId will change (if you space the creations out by a couple of seconds). This is because of the manner in which ObjectIds are created. The 12 bytes of an ObjectId are generated as follows:

0|1|2|3     4|5|6    7|8|   9|10|11 

Timestamp  machine   PID   Increment

The first four bytes of an ObjectId are a timestamp in seconds. This provides a couple of useful properties: The timestamp, when combined with the next five bytes, provides uniqueness at the granularity of a second.Because the timestamp comes first, it means that ObjectIds will sort in roughly insertion order(This is not a strong guarantee). In these four bytes exists an implicit timestamp of when each document was created.

The next three bytes of an ObjectId are a unique identifier of the machine on which it was generated. This is usually a hash of the machine’s hostname. By including these bytes, it is guranteed that different machines will not generate colliding ObjectIds.

To provide uniqueness among different processes generating ObjectIds concurrently on a single machine, the next two bytes are taken from the process identifier (PID) of the ObjectId -generating process. These first nine bytes of an ObjectId guarantee its uniqueness across machines and processes for a single second. The last three bytes are simply an incrementing counter that is responsible for uniqueness within a second in a single process.

This allows for up to 256^3 (16,777,216) unique ObjectIds to be generated per process in a single second.

like image 200
Shahid Hussain Avatar answered Jan 07 '23 19:01

Shahid Hussain