Tools / MongoDB ObjectId ↔ Timestamp

MongoDB ObjectId ↔ Timestamp.

Extract the creation time from MongoDB ObjectIds, or build ObjectIds from dates for range queries.

Runs in your browser

One per line: a 24-character ObjectId (also ObjectId("…") or {"$oid": "…"}), an ISO date, a Unix timestamp or now.

Ctrl/⌘ + Enter to run · Ctrl/⌘ + Shift + C to copy the result

Result

About MongoDB ObjectId ↔ Timestamp

MongoDB ObjectId ↔ Timestamp reads the creation time that every ObjectId carries and builds ObjectIds from dates. The first four bytes of an ObjectId are a Unix timestamp in seconds, so 507f1f77bcf86cd799439011 decodes to 2012-10-17T21:13:27Z. The table also splits out the 5-byte random value and the 3-byte counter that make ids unique, which helps when you are debugging duplicate inserts or checking whether ids came from the same process. Paste ids as bare hex, as ObjectId("...") from the mongo shell or as {"$oid": "..."} from Extended JSON and they are recognised. Enter a date or a Unix timestamp instead and you get the smallest ObjectId for that second, with the remaining sixteen hex digits set to zero, plus a ready-made filter such as { _id: { $gte: ObjectId("6955b9000000000000000000") } }. That is the fastest way to find documents created after a moment when the collection has no createdAt field, because the query uses the _id index. Turn on the random tail when you need realistic test ids instead of lower bounds. Watch for two common mistakes. Timestamps in milliseconds, such as 1767225600000, are read as milliseconds rather than seconds, so check that the date looks right. And the embedded time has one-second precision and comes from the client or driver that created the id, so clock skew between servers can make ids slightly out of order. Dates without a time zone are read as UTC. Ids are decoded in your browser; nothing is sent to a database or server.

How to Use

  1. Paste ObjectIds, ISO dates or Unix timestamps, one per line.
  2. Turn on the random tail if you need test ids instead of query boundaries.
  3. Copy the timestamps, the generated ids or the ready-made $gte query.

Frequently Asked Questions

How accurate is the timestamp inside an ObjectId?

It has one-second precision and records the clock of the machine that generated the id, usually your application server or driver.

Why does the generated ObjectId end in zeros?

It is the lowest possible id for that second, which makes it a correct lower bound for $gte and $lt range queries on _id. Turn on the random tail for realistic ids.

Can ObjectIds represent dates after 2038?

Yes. The timestamp is an unsigned 32-bit value and lasts until 2106-02-07.

Is anything sent to MongoDB?

No. The conversion is pure arithmetic in your browser with no network requests.

Related Tools