MongoDB: structure of the _id field (ObjectId)
Breakdown of the 12 bytes of a MongoDB ObjectId: timestamp, machine, process and counter.
Every MongoDB document has an _id field that makes it unique in its collection. If the application does not provide one at insertion, MongoDB generates an ObjectId for the document.
Structure of an ObjectId
An ObjectId is 12 bytes, written as 24 hexadecimal characters (2 characters per byte):
| Bytes | Content | Hex characters |
|---|---|---|
| 4 | Timestamp of the creation | 8 |
| 3 | Machine identifier | 6 |
| 2 | Process identifier of the mongod server |
4 |
| 3 | Incremental counter | 6 |
Notes
- Recent MongoDB drivers replace the machine and process identifiers by a 5-byte random value. The counter stays on 3 bytes.
- The original note showed a 12-character example. A complete
ObjectIdalways has 24 hexadecimal characters.