Anatomy of an ObjectId
An ObjectId is 12 bytes, written as 24 hex characters. The first 4 bytes are a Unix timestamp in seconds, the next 5 are a random value fixed per process, and the last 3 are a counter that starts at a random number. That layout lets drivers create unique IDs without asking the database, and makes ObjectIds roughly sortable by creation time.
This generator follows the same layout, so the IDs work anywhere MongoDB expects an ObjectId, such as test fixtures, seed data and import files.
Not a secret
ObjectIds reveal when a document was created and are partly predictable from the counter. Never use them as access tokens or to protect private URLs.
Frequently asked questions
How do I read the time from an ObjectId?
Convert the first 8 hex characters to a number. That is the creation time in seconds since 1970. In mongosh, ObjectId(“…”).getTimestamp() does it for you.
Will these collide with IDs my driver creates?
In practice, no. The random 5-byte value differs per process, so IDs from this page and from your application occupy different ranges.