MongoDB ObjectID Generator - Create Unique BSON Identifiers
Generate valid MongoDB ObjectIDs online. Create 1 to 100 unique 12-byte BSON identifiers with a timestamp, random value, and counter, free and in the browser.
MongoDB ObjectID Generator
Documentation
What Is a MongoDB ObjectID?
A MongoDB ObjectID is a 12-byte value that MongoDB assigns by default to the _id field of every document. It is written as 24 lowercase hexadecimal characters, for example 507f1f77bcf86cd799439011. This tool generates between 1 and 100 valid ObjectIDs at a time, directly in the browser, with no database connection needed.
MongoDB ObjectID Structure
An ObjectID is built from three parts, placed one after another:
- Timestamp (4 bytes). The number of seconds since January 1, 1970 (the Unix epoch), stored as an unsigned, big-endian number. It records when the ObjectID was created.
- Random value (5 bytes). A number chosen at random once, when a program starts. Every ObjectID that program creates afterward reuses the same random value, so different computers do not need to coordinate.
- Counter (3 bytes). A number that starts at a random point between 0 and 16,777,215 and rises by 1 for each new ObjectID. If it passes 16,777,215, it wraps back to 0.
Laid end to end, the parts look like this:
1timestamp (8 hex chars) + random value (10 hex chars) + counter (6 hex chars) = 24 hex chars
2Older MongoDB drivers built the middle section from a machine identifier and a process ID rather than a random number. Current drivers, including the logic behind this tool, use one random 5-byte value per process instead.
MongoDB ObjectID Format and Formula
There is no arithmetic formula behind an ObjectID, unlike a percentage or an interest calculation. It is assembled by converting three numbers to hexadecimal text and joining them end to end:
- Take the current time in seconds since the Unix epoch. Write it as an 8-digit hexadecimal number.
- Take the process's fixed random value, a number from 0 up to 2^40 − 1. Write it as a 10-digit hexadecimal number.
- Take the counter's current value, from 0 to 16,777,215. Write it as a 6-digit hexadecimal number, then raise the counter by 1 for the next ID.
- Join the three hexadecimal strings, in that order, into one 24-character string.
Example
Suppose a program starts at Unix time 1,700,000,000 seconds, which is November 14, 2023, 22:13:20 UTC. It picks the random value 3f2a91c8d4 and starts its counter at 12,345.
- Timestamp in hex:
6553f100 - Random value in hex:
3f2a91c8d4 - Counter in hex:
003039
Joining these gives the first ObjectID:
6553f1003f2a91c8d4003039
If the program generates a second ObjectID in the same second, the timestamp and random value stay the same, but the counter rises to 12,346 (00303a):
6553f1003f2a91c8d400303a
Reading an ObjectID also works in reverse. Given 507f1f77bcf86cd799439011, its first 8 characters, 507f1f77, decode to 1,350,508,407 seconds since the epoch, which is October 17, 2012, 21:13:27 UTC.
Common Uses for MongoDB ObjectIDs
- Primary keys. MongoDB puts an ObjectID in a document's
_idfield automatically, unless a different value is supplied. - Creation-time lookup. Because the timestamp is embedded, a program can read a document's approximate creation time without a separate date field.
- Sorting by insertion order. Sorting documents by
_idroughly sorts them by creation time, since the timestamp comes first in the value. - Test and mock data. Developers use generated ObjectIDs to build sample documents or automated tests without running a database.
MongoDB ObjectID vs. UUID
A UUID (Universally Unique Identifier) is a 16-byte value, four bytes larger than an ObjectID. A standard UUID is usually random throughout, so it does not reveal a creation time or sort in creation order. An ObjectID is shorter and roughly time-ordered, which is one reason MongoDB uses it by default, though it does not carry the same formal uniqueness guarantee that a properly generated UUID does.
Frequently Asked Questions
What is a MongoDB ObjectID used for?
It is the default value MongoDB puts in a document's _id field, so every document gets a unique key without extra setup.
Are MongoDB ObjectIDs guaranteed to be unique? Not in a formal, mathematical sense, but collisions are extremely unlikely. Two ObjectIDs would need the same random value and the same counter position in the same second, which normally only happens within a single process.
Can the creation date be read from an ObjectID? Yes. The first 8 hexadecimal characters, 4 bytes, hold the number of seconds since the Unix epoch. Converting that number to a date gives the approximate creation time.
How many ObjectIDs can this generator make at once? Between 1 and 100 per batch. Every ID in a batch shares the same process-wide random value, and each has a counter one higher than the last.
Can MongoDB ObjectIDs be used outside of MongoDB? Yes. Since they are just 12-byte values encoded as hexadecimal text, they work as unique IDs in other systems too, though a database's own ID scheme may fit its needs better.
Is this generator connected to a real MongoDB database? No. It runs entirely in the browser and does not read from or write to any database.
References
- "ObjectId." MongoDB Manual, https://docs.mongodb.com/manual/reference/method/ObjectId/.
- "ObjectId Specification." MongoDB BSON Specifications, https://github.com/mongodb/specifications/blob/master/source/bson-objectid/objectid.md.
- "BSON Types." BSON Specification, http://bsonspec.org/spec.html.