MotionQL

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.

ObjectIdCreated (UTC)Your time zoneUnix secondsRandom (5 bytes)Counter
507f1f77bcf86cd7994390112012-10-17T21:13:27Z10/17/2012, 9:13:27 PM (13 years ago)1350508407bcf86cd7994,427,793
65f1a2b3c4d5e6f708192a3b2024-03-13T12:57:23Z3/13/2024, 12:57:23 PM (2 years ago)1710334643c4d5e6f7081,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.

FAQ

Questions, answered

The first 4 bytes (8 hex characters) are a Unix timestamp in seconds. Paste the id above to see it as a date, or in mongosh call ObjectId("...").getTimestamp().

More free MongoDB tools