MongoDB ObjectId to Timestamp Converter

Decode a MongoDB ObjectId into its creation time and its 12 bytes, turn a date into an ObjectId for _id range queries, or generate fresh ObjectIds.

Client-side only, nothing leaves your browser

Created

UTC (ISO 8601)
2023-11-29T09:03:24.000Z
Unix timestamp (seconds)
1701248604
Local time
Wednesday, November 29, 2023 at 01:03:24 PM GMT+4
Milliseconds
1701248604000

Byte layout

6566fe5c3d5e0f2a1b4c7d8e
Timestamp, 4 bytes
6566fe5c = 1701248604
Random value, 5 bytes
3d5e0f2a1b
Counter, 3 bytes
4c7d8e = 5012878

Get the timestamp in code

mongosh
ObjectId("6566fe5c3d5e0f2a1b4c7d8e").getTimestamp()
Node.js driver
new ObjectId("6566fe5c3d5e0f2a1b4c7d8e").getTimestamp()
Python (bson)
ObjectId("6566fe5c3d5e0f2a1b4c7d8e").generation_time
Aggregation
db.collection.aggregate([{ $project: { createdAt: { $toDate: "$_id" } } }])

How an ObjectId Is Built

Every ObjectId is 12 bytes, written as 24 hex characters. The first 4 bytes count the seconds since the Unix epoch, which is why you can read a creation time from any _id without a separate createdAt field. The next 5 bytes are a random value picked once per client process, and the last 3 bytes are a counter that starts at a random number and increments with each new id. The driver or mongosh builds the id on the client, so the time comes from the client's clock, not the server's.

Quick Reference

  • ObjectId("...").getTimestamp(): creation time as a Date in mongosh
  • ObjectId.createFromTime(seconds): Node.js, an ObjectId with zeros after the timestamp, for range queries
  • ObjectId(seconds): mongosh, the integer replaces the timestamp but the rest stays random
  • ObjectId(...).generation_time: Python bson, a timezone-aware UTC datetime
  • ObjectId.from_datetime(dt): Python bson, for range queries only, never for inserts
  • { $toDate: "$_id" }: aggregation, the date stored in the ObjectId

Frequently asked questions

What is ObjectId in MongoDB?
ObjectId is the default type for the _id field, the primary key every document in a standard collection must have. If you insert a document without an _id, the driver creates an ObjectId for it, and upserts that insert a new document get one too. MongoDB builds a unique index on _id when it creates the collection, and that index cannot be dropped.
MongoDB ObjectId vs UUID: which should I use?
An ObjectId is 12 bytes and starts with its creation time, so ids sort roughly in insert order and you get a created date for free. A UUID is 16 bytes, stored in BSON as binary subtype 4, and mongosh's UUID() generates a random version 4 value with no time inside it. Stick with ObjectId unless another system already uses UUIDs as keys, such as a SQL database or an outside API.
Are MongoDB ObjectIds sortable by creation time?
Sorting on _id is roughly the same as sorting by creation time, but MongoDB's docs say ObjectIds are not necessarily monotonic. The timestamp only has one-second resolution, so ids created in the same second have no guaranteed order, and clients with different system clocks can produce ids out of sequence. If exact order matters, store a separate date field and index it.
Is a MongoDB ObjectId unique across collections?
MongoDB only enforces uniqueness inside one collection, through the unique index on _id, so two collections can hold the same _id without an error. Generated ObjectIds rarely collide in practice, which is why MongoDB's docs describe them as likely unique rather than guaranteed. On a sharded cluster that is not sharded on _id, the application has to keep _id values unique itself.
How do I convert a MongoDB ObjectId to a string?
Call toString() on the ObjectId in mongosh, toHexString() in the Node.js driver, or str(oid) in Python to get the 24-character hex string. Inside an aggregation, { $toString: "$_id" } does the same, which helps when a $lookup joins against a field that stores ids as text. To go back, use ObjectId.createFromHexString() in mongosh or Node.js, or pass the string to ObjectId() in Python.
MongoDB

Need a MongoDB GUI Client?

1bench is a modern database client for MongoDB and 20+ other databases. Query, browse, and manage your data visually.

Learn More