MongoDB ObjectId Generator & Decoder – Create Valid Test IDs Online
Need a realistic MongoDB _id for a fixture, seed file or query test? Use the MongoDB ObjectId Generator above to create structurally valid 12-byte ObjectIds without opening a shell or writing a helper script.
You can generate one ID or a batch, use the current time or choose a historical timestamp, keep one process identifier across the batch, control the starting counter and decode any existing 24-character ObjectId to inspect what is inside it.
The tool is intended for development, testing and debugging. In a production application, the safest normal practice is still to let your MongoDB driver generate ObjectIds because the driver manages the process-specific value and counter for you.
What Is a MongoDB ObjectId?
A MongoDB ObjectId is a 12-byte BSON value commonly used as the default value of the _id field.
When written as hexadecimal text, those 12 bytes become 24 characters.
Example:
507f1f77bcf86cd799439011
The modern ObjectId layout contains three parts:
| Part | Size | Purpose |
|---|---|---|
| Timestamp | 4 bytes | Unix time in seconds |
| Process random value | 5 bytes | Random value unique to a process |
| Counter | 3 bytes | Incrementing value initialized randomly |
| Total | 12 bytes | Complete ObjectId |
The timestamp and counter bytes are stored most-significant-byte first.
How to Use This MongoDB ObjectId Generator
Choose how many ObjectIds you need.
For ordinary fixtures, keep the timestamp set to the current time. If you are building historical data, select a specific date and time.
Next, choose the process identifier.
Leaving it on automatic creates a random five-byte value for the batch. Keeping that value stable makes the generated set resemble IDs created by one running client process.
Set the counter start if you need predictable sequential values within the same timestamp.
Then choose the output format and generate the batch.
You can also paste an existing ObjectId into the decoder. The tool will break it into its timestamp, process-random and counter portions so you can see how the value was formed.
Why Is an ObjectId 24 Characters Long?
The underlying ObjectId is 12 bytes.
Each byte contains 8 bits, giving:
12 × 8 = 96 bits
Hexadecimal represents each byte with two characters.
So:
12 bytes × 2 hex characters = 24 characters
That is why a MongoDB ObjectId string is normally exactly 24 hexadecimal characters using digits 0–9 and letters a–f.
Uppercase hexadecimal can represent the same bytes, although MongoDB commonly displays ObjectIds in lowercase.
A random 24-character hex string may fit the textual length, but that alone does not make it a realistic driver-generated ObjectId. Real ObjectIds have a defined timestamp/process/counter structure.
The 4-Byte ObjectId Timestamp
The first four bytes encode a Unix timestamp in seconds.
That means an ObjectId usually reveals approximately when it was created.
MongoDB can retrieve this value with getTimestamp().
Because the timestamp resolution is one second, many ObjectIds created during the same second share the same first eight hexadecimal characters.
That does not create a collision by itself. The remaining five-byte process value and three-byte counter distinguish IDs created within that period.
The embedded timestamp is useful for debugging and rough sorting, but it should not replace an explicit business date field.
Decode an ObjectId to a Date
If you already have an ObjectId, paste it into the decoder.
The first eight hexadecimal characters represent the four-byte timestamp.
The decoder converts that value from Unix seconds into a human-readable date and time.
This is useful when investigating:
- Old fixture data
- Imported records
- Log entries
- API responses
- Suspicious record order
- Migration output
- Example IDs in documentation
Remember that the decoded time tells you the timestamp encoded in the ID. It does not prove when a document was inserted into its current collection.
ObjectIds can be generated manually or moved between systems.
What Is the 5-Byte Process Random Value?
The middle five bytes are a random value generated for the client-side process.
MongoDB documentation describes this value as unique to the machine and process. It is generated again when the process restarts.
A normal driver keeps it stable while the process remains alive.
That is why this generator lets you keep one process value across a batch.
If you are creating 100 fixture IDs intended to look as though they came from one application process, one stable value produces a more realistic structure than choosing a new five-byte random block for every row.
For normal production writes, leave this job to the driver.
What Is the 3-Byte Counter?
The final three bytes form an incrementing counter.
Three bytes can represent:
2^24 = 16,777,216
different counter values before wrapping.
The counter is initialized to a random value when a process starts and increments for each ObjectId produced by that process.
Together with the timestamp and process value, it helps prevent IDs generated within the same second from colliding.
In a realistic fixture batch, incrementing the counter creates the same structural pattern you would expect from one process generating several IDs close together.
Are MongoDB ObjectIds Sorted by Creation Time?
ObjectIds are roughly sortable by creation time because the timestamp comes first.
However, “roughly” matters.
The timestamp only has one-second resolution. Several processes can create IDs during the same second, each with a different process-random value and counter state.
That means sorting ObjectIds is useful for broad chronology but should not be treated as a perfect global event sequence.
If exact ordering matters, store an explicit timestamp or sequence appropriate to your application.
For newer system designs where millisecond ordering and UUID compatibility are important, compare the UUID v7 Generator as well.
Generate an ObjectId for a Specific Timestamp
Historical ObjectIds are useful in tests.
Suppose you are testing a retention rule that affects documents older than 90 days. You may want fixtures whose IDs carry older timestamps so your test data resembles real MongoDB records.
Choose the required date in the generator and create the batch.
It is useful for pagination tests, time-range examples, migration rehearsals, seed databases and integration fixtures.
Do not create backdated production IDs merely to make a record appear older. Use explicit application fields for business dates.
ObjectId vs UUID v7
Both can work as distributed identifiers, but their structures differ.
| Feature | ObjectId | UUID v7 |
|---|---|---|
| Binary size | 12 bytes | 16 bytes |
| Common text length | 24 | 36 |
| Timestamp resolution | Seconds | Milliseconds |
| Time ordered | Roughly | Yes |
| Native MongoDB default | Yes | No |
| Standard UUID format | No | Yes |
Use ObjectId when MongoDB’s native conventions fit your application.
Use UUID v7 when identifiers need to move cleanly across systems that already understand UUIDs or when millisecond timestamp ordering is useful.
ObjectId vs ULID
ULID is another timestamp-first identifier.
A ULID is 128 bits and uses a 26-character Crockford Base32 representation. Its timestamp has millisecond resolution.
ObjectId is smaller at 96 bits and is deeply associated with MongoDB.
ULID is attractive when you want a sortable identifier that remains compact as text across different databases.
ObjectId is usually simpler when MongoDB itself is the center of the system.
Use the ULID Generator if you want to compare the text length, decoded timestamp and sorting behavior directly.
Should You Manually Generate Production _id Values?
Usually, no.
MongoDB drivers already know how to create ObjectIds correctly.
Manual generation is most useful when you need controlled fixtures, examples, migration data or debugging.
If you decide to supply your own production _id, uniqueness becomes your responsibility. MongoDB requires _id values to be unique within a collection, but your application must still generate a valid non-colliding value.
Do not copy a generated example from documentation into several real records.
Common MongoDB ObjectId Mistakes
Avoid these common assumptions:
- Every 24-character string is a valid ObjectId
- The timestamp has millisecond precision
- ObjectId sorting provides perfect event order
- The ObjectId creation time always equals database insertion time
- A new random process block should be generated for every ID in one process
- The counter can be ignored when imitating realistic batches
- A hand-built production ID is automatically safe because it has the right length
Use the decoder when an unfamiliar ObjectId needs inspection rather than guessing from the string. For a broader entropy source, see the Random Hex String Generator, and see the NanoID Generator for compact alternatives.