How Mongo create unique Id for a document/object?
You have noticed when you store a document in mongo db. Mongo sets the _idproperty by itself with value of long string like this …. 5dbc11af4c23283312490b5c
This _id has 24 characters and every 2 characters represent 1 byte. Essentially mongo creates 12 bytes unique identity for every document.
This 12 bytes string is a group of 4 different identifiers.
- First 4 bytes :- first 4 bytes represents time stamp. This stamp is the time when the document is created.
- Second 3 bytes :- This suite represents machine identifier. Every different machine will have different machine identifier.
- Third 2 bytes :- This suite represents process identifier. If you generate two different documents on same machine but on different processor. So, the process identifier will be different for both documents.
- Fourth 3 bytes :- This suite represents auto increment counter. If we are on same machine, same processor and generate two different documents at same time then this counter bytes will be be different.
So, with these 12 bytes mongo can uniquely identify every document. There are very less chances of two documents are of same ID.
Nice
ReplyDelete