What is a MongoDB ObjectId decoder?
Every document in a MongoDB collection has an _id. When you do not supply one, the driver creates an ObjectId: 12 bytes shown as 24 hexadecimal characters, such as 507f1f77bcf86cd799439011. Those 12 bytes are not random noise. The first four bytes are the creation time in whole seconds since 1970, the next five are a value chosen once per process, and the last three are a counter that goes up by one for each new id. That is why ObjectIds sort roughly by creation time and why you can read a date out of one.
This tool decodes ObjectIds and generates them. Paste one or many and it shows the creation time in UTC and in your time zone, the epoch seconds, the 5-byte random value and the counter. In Generate mode it creates ObjectIds for the current moment or for a date you choose, and it writes the matching range query for the mongo shell, which is what you need to find documents created between two dates without a separate date field.
It runs in your browser and nothing is uploaded. Know what the date means. It is the clock of the machine that created the id, in whole seconds, and nothing signs it, so it is a good hint about when a document was created but not proof. The random part is not a secret either. Do not use ObjectIds as tokens or passwords. If you need an id that sorts by time and is made for the web, look at the ULID generator; for fully random ids, the UUID generator.
How to use it
- Open the page. It loads the example from the MongoDB manual,
507f1f77bcf86cd799439011, and shows its decoded fields. - Paste your own ObjectIds. You can paste several, one per line, or copy
ObjectId("…")straight from the shell or from a log. Quotes and brackets are removed for you. - Read the result. For one id you get the UTC time, your local time, the epoch seconds, the random value and the counter. For several ids you get one row per id with its time.
- Click a copy button to copy a field. To check the date in another format, use the Date converter button below the result.
- Switch to Generate to make new ids. Type a date such as
2024-01-01T00:00:00Z, epoch seconds or milliseconds, or press Now, then choose how many ids you want. - Copy the lowest
_idof that second or the ready-made query. Fill the Until field to get a query with both bounds.
Examples
The documented example
507f1f77bcf86cd799439011
time: 2012-10-17T21:13:27Z
random: bcf86cd799
counter: 4427793 (0x439011)This is the id used in the MongoDB documentation for getTimestamp(). The first eight characters, 507f1f77, are 1350508407 seconds after 1970.
Documents created in January 2024
Generate with the date 2024-01-01 and Until 2024-02-01. The query is { _id: { $gte: ObjectId("659200800000000000000000"), $lt: ObjectId("65badf000000000000000000") } } with the bounds computed for you. It uses the default _id index and needs no extra field.
Checking when a record was created
A support ticket mentions the document 5349b4ddd2781d08c09890f3. Decode it to learn it was created on 12 April 2014 at 21:49:17 UTC, without opening the database.
A batch for tests
Generate 20 ids for a fixed second. They share the same random value and the counter goes up by one, like ids made by a real process, which is useful when you need ordered ids in a fixture.
Common mistakes
- Pasting an id with the wrong length. An ObjectId has exactly 24 hex characters. A missing or extra character is the most frequent error, and the tool says which id and how many characters it found.
- Using non-hex characters. Only 0 to 9 and a to f are allowed. A letter such as
ofor zero orgis reported by name. - Reading the time in the wrong zone. The time is UTC. Your local time is shown next to it, but the database stores UTC.
- Expecting milliseconds. An ObjectId stores whole seconds. Two ids made in the same second differ only in the counter.
- Trusting the date for security. A client can set any value, so do not use the embedded time for audits or access decisions.
- Mixing up a range query bound.
$gtewith the lowest id of the first second and$ltwith the lowest id of the next second after the end include exactly the seconds you want.
Frequently asked questions
Is the date inside an ObjectId in UTC?
Yes. It is the number of seconds since 1970-01-01T00:00:00Z, stored as a 4-byte number. The tool shows UTC and your local time.
What are the 5 random bytes?
A value chosen once when the process starts. Before MongoDB 3.4 they were a machine identifier and a process id, so the tool also shows that older reading as a hint.
Can two ObjectIds be equal?
Only by an extraordinary coincidence. Within a process the counter makes ids unique, and different processes use different random values.
Can I use ObjectIds as public tokens?
No. They are predictable: the time is known and the counter goes up by one. Use a random token from the token generator for secrets.
How do I turn the timestamp into other date formats?
Use the date converter. The button under the result opens it with the decoded time already filled in.
Does it work with ObjectIds from Mongoose, Atlas or Compass?
Yes. They all use the same 12-byte format, so ids copied from any of them decode the same way.