MongoDB ObjectId Generator & Decoder

Generate MongoDB ObjectIds and decode any 24-character id into its timestamp, random value and counter.

โ€”

Created (UTC)
โ€”
Local time
โ€”
Relative
โ€”
Unix seconds
โ€”
Byte fields
Timestamp hex
โ€”
Random value
โ€”
Counter
โ€”
Build an ObjectId for a specific date (range queries)

The five random bytes and the three counter bytes are zeroed, so the result is the smallest ObjectId that can exist for that second โ€” exactly what you want as a boundary in { _id: { $gte: ObjectId("โ€ฆ") } }. Use it to filter a collection by creation date without storing a separate createdAt field.

No uploads. Your files stay on your device.

Free forever, no sign-up, no cookies. Buy me a coffee

How it works

A MongoDB ObjectId is 12 bytes, printed as 24 hexadecimal characters, and it is laid out exactly as the BSON ObjectId specification describes: a 4-byte big-endian count of seconds since the Unix epoch, then a 5-byte value that is random per process, then a 3-byte big-endian counter that starts at a random number and increments on every id the driver hands out. This page implements that layout literally โ€” decoding just slices the hex string into those three fields, so the date you see is the second the document's id was minted, accurate to one second and nothing finer. Older drivers (before MongoDB 3.4) split the middle bytes into a 3-byte machine identifier and a 2-byte process id; those ids still decode correctly here because the timestamp and counter never moved.

Generating works the same way in reverse. The five random bytes are drawn once per page load from crypto.getRandomValues(), mimicking the per-process value a real driver uses, and the counter increments with every click, so a burst of ids is unique and monotonically ordered within the same second. Nothing is sent anywhere: no database connection, no API, no account. Paste an id from a log, an error message or a URL and it decodes as you type, and the page keeps working with your network switched off.

Frequently asked questions

What are the 12 bytes of a MongoDB ObjectId?

A BSON ObjectId is 4 bytes of big-endian Unix time in seconds, then 5 bytes that are random once per process, then a 3-byte big-endian counter that starts at a random value and increments for every id the driver creates. Printed as hex that is 24 characters. Drivers before MongoDB 3.4 split the middle five bytes into a 3-byte machine identifier and a 2-byte process id, so those older ids still decode to the same date and counter here.

Can I get the creation date out of an ObjectId?

Yes. The first eight hex characters are the number of seconds since 1 January 1970, so paste the id above and the UTC time, your local time and the Unix seconds appear straight away. The resolution is one second, not milliseconds, and the date is the moment the id was generated by the application, which is normally when the document was inserted.

How do I query MongoDB by date using _id?

Open the date builder on this page, pick a UTC date and time, and it produces the smallest ObjectId that can exist for that second by zeroing the random and counter bytes. Use it as a boundary, for example { _id: { $gte: ObjectId("659200800000000000000000") } }, to filter by creation date without adding a separate createdAt field.

Report a bug