MongoDB
timestamp extraction
ObjectId
database operations
data engineering

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.

python
1from bson import ObjectId
2
3obj_id = ObjectId("6515e77e1c3dc2cdd0b7e8a0")
4print(obj_id.generation_time)

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:

python
1from bson import ObjectId
2
3obj_id = ObjectId("6515e77e1c3dc2cdd0b7e8a0")
4print(obj_id.generation_time.isoformat())

JavaScript and the Mongo Shell

In the Mongo shell or compatible JavaScript environments, ObjectId has a convenience method too.

javascript
const id = ObjectId("6515e77e1c3dc2cdd0b7e8a0");
print(id.getTimestamp());

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:

text
6515e77e1c3dc2cdd0b7e8a0
^^^^^^^^

Those first eight hex digits represent the Unix timestamp in seconds.

In Python, manual decoding looks like this:

python
1from datetime import datetime, timezone
2
3hex_id = "6515e77e1c3dc2cdd0b7e8a0"
4timestamp_seconds = int(hex_id[:8], 16)
5dt = datetime.fromtimestamp(timestamp_seconds, tz=timezone.utc)
6
7print(timestamp_seconds)
8print(dt.isoformat())

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 _id field is assigned outside MongoDB and sent later
  • documents use custom _id values 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.

python
1from zoneinfo import ZoneInfo
2from bson import ObjectId
3
4obj_id = ObjectId("6515e77e1c3dc2cdd0b7e8a0")
5local_time = obj_id.generation_time.astimezone(ZoneInfo("America/Toronto"))
6print(local_time)

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 ObjectId stores a timestamp in its first four bytes.
  • In Python, ObjectId.generation_time is 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.

Course illustration
Course illustration

All Rights Reserved.