Skip to content
gohacks.dev
Databases

MongoDB ObjectId Decoder

Decode a MongoDB ObjectId into its creation time, process random value and counter, or generate new ObjectIds in your browser.

Free · No sign-up · Your inputs stay in your browser

Input

Decode or generate a MongoDB ObjectId

Output

2024-03-13T12:57:23.000Z

Created (UTC)2024-03-13T12:57:23.000Z
Unix seconds1710334643
Process randomc4d5e6f708
Counter1649211
Find by timedb.items.find({ _id: { $gte: ObjectId("65f1a2b30000000000000000") } })

An ObjectId is 4 bytes of seconds since the Unix epoch, 5 random bytes chosen once per process, and a 3-byte counter. The timestamp is when the id was generated by the driver, not when the document was written, and it has one-second resolution. Range-querying _id by time works because the timestamp comes first.

Runs in this browser. Nothing you paste is sent to a server.

A free created_at, with limits

Because the timestamp comes first, ObjectIds sort roughly by creation time and ObjectId.getTimestamp() gives a creation date without a separate field. The time is when the driver generated the id, which is usually but not always when the document was inserted, and it has one-second resolution. See the ObjectId reference.

Not a secret

Anyone who sees an ObjectId can read when it was created, and ids generated close together are predictable. Do not use them as unguessable tokens in URLs for private resources.

Share

Frequently asked questions

How do I get the timestamp of an ObjectId in the shell?
ObjectId("65f1a2b3c4d5e6f708192a3b").getTimestamp() in mongosh returns an ISODate.
Are ObjectIds globally unique?
In practice yes: the 5-byte random value differs per process and the counter per id, so collisions need two processes with the same random value generating ids in the same second.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools