0
0
MongoDBquery~10 mins

ObjectId and how it is generated in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ObjectId and how it is generated
Start: Need unique ID
Generate Timestamp (4 bytes)
Generate Machine ID (3 bytes)
Generate Process ID (2 bytes)
Generate Counter (3 bytes)
Combine all parts into 12-byte ObjectId
Use ObjectId as unique document ID
ObjectId is created by combining time, machine, process, and counter parts to make a unique 12-byte ID.
Execution Sample
MongoDB
ObjectId() // generates a new unique 12-byte ID
This code creates a new ObjectId by combining timestamp, machine ID, process ID, and counter.
Execution Table
StepPart GeneratedValue (Hex)Description
1Timestamp64E3A2F4Current time in seconds since epoch (4 bytes)
2Machine ID5F1D3AUnique machine identifier (3 bytes)
3Process ID1A2BCurrent process identifier (2 bytes)
4Counter000001Incrementing counter to avoid duplicates (3 bytes)
5Combine64E3A2F45F1D3A1A2B000001Final 12-byte ObjectId
💡 All parts combined to form a unique ObjectId for document identification
Variable Tracker
PartInitialAfter GenerationFinal
TimestampN/A64E3A2F464E3A2F4
Machine IDN/A5F1D3A5F1D3A
Process IDN/A1A2B1A2B
Counter011
ObjectIdN/AN/A64E3A2F45F1D3A1A2B000001
Key Moments - 3 Insights
Why does ObjectId include a timestamp?
The timestamp (step 1 in execution_table) ensures ObjectIds are roughly ordered by creation time and helps uniqueness.
What prevents two ObjectIds from being the same on the same machine and process?
The counter (step 4) increments for each ObjectId generated in the same second, avoiding duplicates.
Why is the machine ID part important?
The machine ID (step 2) ensures ObjectIds generated on different machines are unique even if timestamp and counter match.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the length in bytes of the Process ID part?
A3 bytes
B2 bytes
C4 bytes
D1 byte
💡 Hint
Check the 'Part Generated' and 'Value (Hex)' columns in the execution_table for Process ID
At which step is the counter value generated?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look for the row where 'Counter' is generated in the execution_table
If the machine ID was the same but the process ID changed, what would happen to the ObjectId?
AIt would be the same
BOnly the timestamp changes
CIt would be completely different
DOnly the counter changes
💡 Hint
Refer to how ObjectId combines machine ID and process ID in the execution_table
Concept Snapshot
ObjectId is a 12-byte unique ID.
It combines 4 parts: timestamp (4 bytes), machine ID (3 bytes), process ID (2 bytes), and counter (3 bytes).
Timestamp orders IDs by creation time.
Machine and process IDs ensure uniqueness across machines and processes.
Counter avoids duplicates within the same second.
Full Transcript
ObjectId in MongoDB is a unique identifier for documents. It is 12 bytes long and made by combining four parts: a 4-byte timestamp representing the current time, a 3-byte machine identifier unique to the computer, a 2-byte process ID for the running program, and a 3-byte counter that increments with each new ObjectId. This combination ensures each ObjectId is unique and roughly ordered by creation time. The timestamp helps sort documents by creation time. The machine and process IDs prevent collisions across different servers and processes. The counter prevents duplicates when multiple ObjectIds are created quickly on the same machine and process. This process creates a unique 12-byte hexadecimal string used as the document's ID.