Every MongoDB ObjectId starts with the second it was created. Paste one or many ObjectIds to see their creation dates in UTC and local time, or pick a date to get the matching ObjectId bounds for fast _id range queries.
Last updated:
ObjectId to date
Date to ObjectId
Advertisement
Advertisement
How to use the MongoDB ObjectId to Timestamp
Paste an ObjectId into the decoder, with or without the ObjectId("…") wrapper.
Read the creation time in UTC, your local time zone and as a Unix timestamp, plus the counter and random parts.
Decode many at once by pasting one ObjectId per line; the tool lists each with its date and lets you download a CSV.
Go the other way: choose a date and time (local or UTC) to get the smallest ObjectId for that second.
Build a range query by adding an end date, then copy the generated find() query into mongosh or your driver.
ObjectId structure
A MongoDB ObjectId is 12 bytes, shown as 24 hexadecimal characters. Example: 507f1f77bcf86cd799439011.
Bytes
Hex chars
Field
In the example
0-3
1-8
Timestamp: seconds since the Unix epoch, big-endian
507f1f77 = 1350508407 = 2012-10-17 21:13:27 UTC
4-8
9-18
Random value, unique per machine and process (older drivers: 3-byte machine id + 2-byte process id)
bcf86cd799
9-11
19-24
Counter, starting at a random value and incremented for each new ObjectId
Because the timestamp is the most significant part of the value, ObjectIds compare in time order. An ObjectId made of a timestamp followed by sixteen zeros is the smallest possible id for that second, so {_id: {$gte: ObjectId(startHex + "0000000000000000"), $lt: ObjectId(endHex + "0000000000000000")}} returns every document whose id was created in that interval, using the default _id index. Remember that the boundary is in UTC seconds; if you pick a local time above, the tool converts it to UTC before building the id.
Frequently asked questions
How do I get the timestamp from a MongoDB ObjectId?
Take the first 8 hex characters and convert them from hexadecimal to decimal: that is the creation time in seconds since 1 January 1970 UTC. For 507f1f77bcf86cd799439011, 507f1f77 = 1350508407, which is 17 October 2012, 21:13:27 UTC. In mongosh you can call ObjectId("…").getTimestamp().
What is inside a MongoDB ObjectId?
An ObjectId is 12 bytes: a 4-byte big-endian timestamp in seconds, a 5-byte random value generated once per process, and a 3-byte counter that starts at a random value and increments. Drivers before MongoDB 3.4 used a 3-byte machine identifier plus a 2-byte process id instead of the 5-byte random value.
How do I query MongoDB documents by creation date using _id?
Create an ObjectId whose timestamp is your start date and whose other 16 hex digits are zeros, then query {_id: {$gte: ObjectId(start), $lt: ObjectId(end)}}. Because _id is always indexed, this is fast even without a separate createdAt field. The generator on this page writes the query for you.
Is the ObjectId timestamp accurate?
It has one-second precision and comes from the clock of the machine that created the ObjectId, which is usually the application server rather than the database. ObjectIds can also be supplied by clients, so treat the time as when the id was generated, not proof of when a document was inserted.
Are ObjectIds sortable by time?
Roughly. Sorting by _id orders documents by creation second, but ObjectIds created within the same second on different machines are ordered by their random value, not by exact creation order.
When will ObjectId timestamps run out?
The 4-byte unsigned timestamp can represent dates up to 7 February 2106, 06:28:15 UTC. MongoDB treats the field as unsigned, so there is no 2038 problem.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.