ObjectId decoder and generator
Decode a MongoDB ObjectId into its creation time, random value and counter, decode many at once, or generate ObjectIds for any date to query by time range.
Runs entirely in your browser. Nothing you paste is sent to any server.
| ObjectId | Created (UTC) | Your time zone | Unix seconds | Random (5 bytes) | Counter |
|---|---|---|---|---|---|
| 507f1f77bcf86cd799439011 | 2012-10-17T21:13:27Z | 10/17/2012, 9:13:27 PM (13 years ago) | 1350508407 | bcf86cd799 | 4,427,793 |
| 65f1a2b3c4d5e6f708192a3b | 2024-03-13T12:57:23Z | 3/13/2024, 12:57:23 PM (2 years ago) | 1710334643 | c4d5e6f708 | 1,649,211 |
Timestamp · random value · counter
Generate ObjectIds
Like a driver, each batch uses one random process value and a counter that starts at a random number.
Query by creation time
Boundary ids have zero random and counter bytes, so $gte includes every id created in that second. Works with the default _id index, no extra index needed.
Do this against your real data with MotionQL
Build _id range filters in MotionQL's visual query builder or shell-syntax filter bar, and browse the results in tree, table and JSON views. Pro is free for a year.
How a MongoDB ObjectId is built
An ObjectId is 12 bytes, usually written as 24 hexadecimal characters:
- 4-byte timestamp: seconds since 1970-01-01 UTC, big-endian, so ids sort roughly by creation time.
- 5-byte random value: chosen once per process, unique to the machine and process.
- 3-byte counter: incremented for each new id, starting from a random value.
The 4-byte timestamp runs out in February 2106. Because the timestamp comes first, sorting by _id is a cheap way to get documents in insertion order, and a range on _id is a cheap way to select documents by creation date.
Questions, answered
More free MongoDB tools
Connection string builder
Build or parse mongodb:// and mongodb+srv:// URIs. Split a pasted URI into hosts, credentials and options, catch common mistakes, and copy a correctly encoded string with the password masked.
Open toolExtended JSON converter
Convert MongoDB documents between canonical Extended JSON, relaxed Extended JSON and mongosh shell syntax, keeping BSON types like ObjectId, Date, Long and Decimal128 intact.
Open toolBSON size calculator
Paste a document to get its exact BSON size in bytes, see how close it is to MongoDB's 16 MB document limit, and find the fields taking up the most space.
Open toolExplain plan analyzer
Paste the output of explain() to see the plan as a tree, catch collection scans and in-memory sorts, compare documents examined with documents returned, and get an index suggestion.
Open toolOperators cheat sheet
Searchable reference of MongoDB query, update and aggregation operators and pipeline stages, each with example syntax and a link to the official documentation.
Open tool