MongoDB & NoSQL (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is an ObjectId (`_id`) and what are its 12 bytes composed of?
junior LevelDocker & Containers
In Simple Words:
Every MongoDB document requires a unique primary key _id. By default, MongoDB generates a 12-byte ObjectId:
- 4 bytes: Unix epoch timestamp in seconds (provides natural chronological ordering).
- 5 bytes: Random unique process identifier per machine/process.
- 3 bytes: Incrementing counter initialized to a random value.
- Benefit: Globally unique without coordinating across distributed nodes! You can extract the creation timestamp directly from
_id.getTimestamp().