How to Get the Date from a MongoDB ObjectId: The 12 Bytes Explained
Last updated: · 7 min read
MongoDB gives every document inserted without an _id field a 12-byte ObjectId. It is written as 24 hexadecimal characters and looks random, but its first 4 bytes record the moment the identifier was generated, to the second. That makes it possible to tell roughly when a record was created even in collections that never stored a createdAt field. This guide explains the three parts of an ObjectId, how the date is worked out, how far that date can be trusted, and how to build a boundary id for querying by date range. The values in the example are real output from the MongoDB ObjectId Decoder; the value is decoded in your browser and no database is contacted.
Formula
ObjectId = 12 bytes = 24 hexadecimal characters characters 1–8 → 4 bytes timestamp: seconds since the Unix epoch (big-endian) characters 9–18 → 5 bytes random value: generated once per client-side process characters 19–24 → 3 bytes counter: increments per process, starts at a random value (big-endian) Creation time = hex(first 8 characters) seconds → UTC date Counter range = 0 … 16,777,215 (2^24 − 1) Largest time = ffffffff = 4,294,967,295 s
According to the MongoDB manual, an ObjectId has three parts: a 4-byte timestamp holding the seconds since the Unix epoch (1 January 1970, UTC), a 5-byte random value generated once per client-side process, and a 3-byte counter, also kept per process, that starts at a random value and increments. For the timestamp and the counter the most significant byte comes first, which is why the first eight hex characters convert straight into a number of seconds. The tool converts those eight characters from hex to decimal, writes the seconds as an ISO 8601 UTC date, shows the next ten characters as they are and the last six as a decimal number. The identifier is generated by the driver, on the machine running the application rather than on the server, so the time reflects that machine's clock.
What the inputs mean
- ObjectId
- 24 hexadecimal characters (0–9, a–f); case does not matter and the result is shown in lower case. The forms ObjectId("…") and new ObjectId('…'), as copied from the shell or Compass, are accepted too. Leading and trailing spaces are trimmed; values shorter or longer than 24 characters are rejected.
Worked example: Decoding 507f1f77bcf86cd799439011
- Input: ObjectId = 507f1f77bcf86cd799439011, an identifier that appears as an example throughout MongoDB's documentation. It splits into 507f1f77 (time), bcf86cd799 (random value) and 439011 (counter).
- Time: hex 507f1f77 is 5 × 16^7 + 0 × 16^6 + 7 × 16^5 + 15 × 16^4 + 1 × 16^3 + 15 × 16^2 + 7 × 16 + 7 = 1,350,508,407 seconds, which is 17 October 2012 at 21:13:27 UTC.
- Counter: hex 439011 is 4 × 16^5 + 3 × 16^4 + 9 × 16^3 + 0 × 16^2 + 1 × 16 + 1 = 4,427,793.
- The tool prints five lines: ObjectId: 507f1f77bcf86cd799439011, Created at (ISO 8601, UTC): 2012-10-17T21:13:27Z, Unix time (seconds): 1350508407, Random value (5 bytes, hex): bcf86cd799 and Counter (3 bytes, decimal): 4427793
- Paste the same value the way the shell prints it, ObjectId("507F1F77BCF86CD799439011"), in upper case, and the result does not change; the tool strips the wrapper and lower-cases the letters.
- A boundary id for date ranges: set the time part to the moment you want and fill the remaining 16 characters with zeros to get the smallest ObjectId for that second. For 65a1b2c30000000000000000 the tool shows Created at (ISO 8601, UTC): 2024-01-12T21:44:35Z; documents whose _id is greater than this value have identifiers generated after that moment.
- Upper limit: for ffffffffffffffffffffffff the tool shows Created at (ISO 8601, UTC): 2106-02-07T06:28:15Z and Counter (3 bytes, decimal): 16777215. The tool reads the first four bytes as an unsigned number; this is the largest value a four-byte seconds field can hold.
- Invalid input: drop the last character (23 characters) or type a non-hex letter such as g, and the tool answers: Invalid ObjectId: it must be 24 hexadecimal characters (example: 507f1f77bcf86cd799439011).
Result: 507f1f77bcf86cd799439011 → Created at (ISO 8601, UTC): 2012-10-17T21:13:27Z; Unix time (seconds): 1350508407; Random value (5 bytes, hex): bcf86cd799; Counter (3 bytes, decimal): 4427793
🍃 MongoDB ObjectId Decoder (Timestamp)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
The date you read from an ObjectId is when the identifier was generated, not when the document was. In most applications the two coincide because the driver creates the id at insert time, but in applications that generate ids ahead of time, import data from another system or set _id by hand, the gap can be hours or years. The date has one-second resolution and depends on the client's clock. MongoDB's own documentation notes that ObjectId values should increase over time but are not necessarily monotonic: values created within the same second have no guaranteed order, and different clients may have different clocks. Sorting by _id is therefore only roughly equivalent to sorting by creation time. The time a document was last modified is not in the identifier at all; that needs a field of its own. Bear in mind, too, that sharing a URL containing an ObjectId also shares when the record was created.
Common mistakes
- Reading the ObjectId date as the document's last update time. The identifier is generated once and never changes when the document does; keep a separate field for update times.
- Reading the date as local time. The timestamp is UTC and the tool shows UTC; apply your own offset if you need local time.
- Expecting millisecond precision. The time part is whole seconds; for identifiers generated within the same second the order is decided by the random value and the counter, not by time.
- Using an ObjectId as an unguessable secret. The time part is public, the random value is constant for the life of the process and the counter goes up by one, so neighbouring ids from one process look very much alike. Do not base authorisation on the id being secret.
- Confusing a 24-character string with the ObjectId type. The string "507f1f77…" and ObjectId("507f1f77…") are different BSON types; a query with the wrong type finds nothing.
- Interpreting the middle 5 bytes as a machine id. The current MongoDB manual defines this part as a random value generated per process; it changes when the process restarts and does not reliably identify a server.
- Treating a sort on _id as strict chronological order. There is no ordering guarantee within a second or across clients; if you need strict order, use a dedicated time field or a sequence number.
Frequently asked questions
How do I get the timestamp from an ObjectId?
Convert the first eight hex characters to a decimal number; that is the number of seconds since the Unix epoch. For example, 507f1f77 → 1350508407 → 2012-10-17T21:13:27Z. In the MongoDB shell, ObjectId("…").getTimestamp() returns the same information. The tool does the conversion for you and shows the other parts as well.
Is the date in an ObjectId reliable?
To the second, yes, as long as the clock of the client that generated the id was right and the id was generated at insert time. If the clock was wrong, the id was supplied by hand or the data was migrated from another system, the date can be misleading. Where an exact time matters, for auditing for instance, use a separate timestamp field written by the server.
How do I query documents created after a certain date using _id?
Convert the Unix seconds of that moment to an eight-character hex number, append sixteen zeros, and query for _id values greater than the result. This works for collections where ids are generated at insert time, and it is fast because it uses the _id index.
Is an ObjectId unique?
By design a collision is very unlikely: within one second, a process with a given 5-byte random value can produce 16,777,216 different counter values. The real guarantee, though, is the unique index on _id; the database returns an error if a second document with the same id is inserted.
What is the difference between an ObjectId and a UUID?
An ObjectId is 12 bytes and specific to MongoDB; a UUID is 16 bytes and a general standard defined in RFC 9562. Both can be generated on the client without asking the server. The time part of an ObjectId has one-second resolution, that of a version 7 UUID one millisecond.
Is ObjectId affected by the year 2038 problem?
The tool reads the first four bytes as an unsigned number; read that way, the field lasts until 7 February 2106 and the value 7fffffff (19 January 2038) is not a special limit. Check your driver's documentation to confirm that it interprets the field the same way.
Is the id I paste sent anywhere?
No. Decoding is done by JavaScript on the page, in your browser; the tool does not connect to any database and does not check whether the id actually exists.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.