Skip to content

MongoDB ObjectID to Timestamp

Paste a MongoDB ObjectId and instantly see its embedded creation time, a byte-by-byte breakdown, and a ready-to-paste query for finding documents by date.

What's inside a MongoDB ObjectID?

Every MongoDB document has an _id field, and unless you provide one yourself, MongoDB fills it with a 12-byte value called an ObjectID — a string like 507f1f77bcf86cd799439011. Those 12 bytes aren't random from end to end: the first 4 encode a Unix-seconds timestamp, so every ObjectID quietly carries the moment it was created.

A few things trip people up when reading that timestamp. It's precise only to the second, not the millisecond. It reflects when the ID was generated — practically always document creation — and never changes on a later update. And it's always UTC; there's no timezone stored anywhere in the value.

Finding documents by date without a createdAt field

Because the timestamp lives inside _id itself — and _id is indexed by default — you can query a date range without a separate createdAt field or index at all. Open the "Generate an ObjectID for a date-range query" panel above, pick a date, and copy a ready-to-run query for mongosh, Node.js, Python, Java, C#, or PHP.

One caveat: an ObjectID built this way is for querying only. Its random and counter bytes are zero-filled rather than driver-generated, so it isn't guaranteed unique — never insert a document using it as the _id.

Frequently asked questions

What is a MongoDB ObjectID?

A 12-byte identifier MongoDB automatically generates for a document's _id field unless you supply your own. It's built from a 4-byte creation timestamp, a 5-byte random value unique to the machine and process that generated it, and a 3-byte incrementing counter — which is why ObjectIDs sort roughly in creation order.

How do I extract the timestamp from an ObjectID?

The first 8 hex characters (4 bytes) are a Unix-seconds timestamp. In mongosh you can call ObjectId("...").getTimestamp(), which returns it as a Date. This tool decodes the same bytes instantly and shows UTC, local time, relative time, ISO 8601, and both Unix units.

Is the ObjectID timestamp accurate to the millisecond?

No — only to the second. If your application needs millisecond precision, store a separate createdAt field written at insert time instead of relying on the ObjectID.

Does the ObjectID timestamp update when a document is modified?

No. It reflects only when the _id was generated (almost always document creation), never later updates. Track modification time with your own updatedAt field.

Is the ObjectID timestamp in UTC or local time?

Always UTC. It's a raw Unix-seconds value with no timezone attached — any "local time" you see displayed is applied afterward by the driver, shell, or this tool, purely for reading convenience.

Can I query MongoDB by date using just the ObjectID?

Yes. Because the timestamp is embedded in _id, and _id is indexed by default, you can query date ranges without a separate createdAt field or index. Use the range-query generator on this page to get a ready-to-paste query in mongosh, Node.js, Python, Java, C#, or PHP.

Can I create my own ObjectID with a custom timestamp?

You can, but only for querying — never for inserting a new document. A hand-built ObjectID's random and counter bytes are typically zero-filled, so it isn't guaranteed unique the way a driver-generated one is; using it as a real _id risks a collision.

What changed in the ObjectID structure in MongoDB 4.0?

Older documentation describes bytes 5–9 as a 3-byte machine identifier plus a 2-byte process ID. Since the MongoDB 4.0-era BSON spec update, those same 5 bytes are documented simply as "a random value unique to the machine and process," regenerated if the process restarts — the timestamp bytes and their meaning are unchanged.

Related tools

Smart paste

Paste any timestamp or date, or search for a tool