Getting timestamp from mongodb id
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A MongoDB ObjectId encodes a creation timestamp in its first four bytes, so you can often recover the approximate creation time directly from _id. This is useful for debugging, migration work, and rough time-based analysis. The important qualifier is “approximate”: the timestamp has one-second resolution and reflects ObjectId generation time, not necessarily the exact database insert moment.
What an ObjectId Contains
A standard MongoDB ObjectId is 12 bytes long. Its layout is commonly described as:
- 4 bytes: Unix timestamp in seconds
- 5 bytes: process- or host-related randomness
- 3 bytes: incrementing counter
The first four bytes are what make timestamp extraction possible.
That means two important things follow immediately:
- ObjectIds are roughly sortable by creation time
- the embedded time is only stored to the nearest second
So you can use the value for ordering and inspection, but not as a perfect audit timestamp.
Python Example with PyMongo
If you already have an ObjectId, PyMongo makes extraction easy.
generation_time returns a timezone-aware UTC datetime object.
That is usually the best answer in Python because it avoids manual byte parsing.
If you need a formatted string:
JavaScript and the Mongo Shell
In the Mongo shell or compatible JavaScript environments, ObjectId has a convenience method too.
That returns a Date object representing the ObjectId timestamp.
This is especially handy for quick database inspection without writing a separate parsing utility.
Manual Extraction Logic
If you want to understand what is happening under the hood, the timestamp comes from the first eight hex characters of the ObjectId, because four bytes equals eight hex digits.
Example:
Those first eight hex digits represent the Unix timestamp in seconds.
In Python, manual decoding looks like this:
This is useful when you need to work with raw strings or implement custom tooling.
What the Timestamp Does Not Guarantee
A common misconception is that the extracted time is the exact insert time stored by MongoDB. It is not.
It is the time when that ObjectId value was generated. Those times are often very close, but they are not conceptually identical.
You can see differences when:
- the application generates an ObjectId before the actual insert
- the
_idfield is assigned outside MongoDB and sent later - documents use custom
_idvalues rather than ObjectId - replication or later processing changes the observed write timeline
If you need an authoritative business timestamp, store a dedicated field such as createdAt.
Time Zone Considerations
The embedded timestamp is UTC-based. If you display it to users in local time, convert it deliberately.
This matters when investigating production data across regions.
Practical Uses
Developers commonly extract the timestamp from _id for:
- rough age checks on documents
- quick sorting and debugging
- backfill scripts that need a fallback creation estimate
- migration audits on collections that forgot to store
createdAt
Those are good uses as long as you remember the value is derived metadata, not an explicit business timestamp.
Common Pitfalls
The biggest pitfall is assuming every _id field is an ObjectId. Some collections use UUIDs, strings, or application-defined identifiers.
Another issue is treating the embedded time as exact insert time rather than ObjectId generation time.
Developers also sometimes forget the one-second resolution and overinterpret small differences between documents.
Finally, always remember the extracted value is in UTC unless you convert it.
Summary
- A MongoDB
ObjectIdstores a timestamp in its first four bytes. - In Python,
ObjectId.generation_timeis the easiest way to extract it. - In the Mongo shell,
ObjectId(...).getTimestamp()gives the same information conveniently. - The timestamp is approximate creation metadata, not a perfect insert audit field.
- If exact business timing matters, store a dedicated timestamp field explicitly.

