Developer Tool
Every default MongoDB _id has a creation timestamp hidden in its first 4 bytes. Paste one below to pull it out — no shell, no driver, no query.
Works with or without quotes, and with or without an ObjectId(...) wrapper pasted from a shell or log.
A MongoDB ObjectId is 12 bytes (24 hex characters) long. The first 4 bytes are a big-endian Unix timestamp in seconds — the moment the ID was generated. Modern drivers fill the remaining 8 bytes with 5 bytes of process-specific randomness and a 3-byte incrementing counter, purely to keep IDs unique when many are minted in the same second on the same process; older driver versions used a machine identifier and process ID instead, but the first 4 bytes have always meant the same thing.
That means if your documents use the default auto-generated _id, you already have a reliable creation timestamp for every record without needing a separate createdAt field — you just have to know where to look.
function objectIdToDate(oid) {
const hex = oid.replace(/^ObjectId\(['"]?|['"]?\)$/g, '').trim();
const seconds = parseInt(hex.substring(0, 8), 16);
return new Date(seconds * 1000);
}
objectIdToDate('507f1f77bcf86cd799439011');
// -> 2012-10-17T21:13:27.000Z
Decoding a Discord or Twitter/X snowflake ID instead? Those use the same "timestamp packed into the ID" idea, just with milliseconds and a different layout — see the Snowflake ID Decoder.
Reference
The first 4 bytes (8 hex characters) are a big-endian Unix timestamp in seconds. Convert just those 8 characters from hex to decimal to get the creation time. Paste the full ObjectId above to do it instantly.
Yes, as long as it uses the default auto-generated ObjectId for _id rather than a custom string or UUID — no separate createdAt field required.
After the 4-byte timestamp, current drivers add 5 bytes of process-specific random value and a 3-byte counter — 12 bytes total. Only the first 4 matter for the timestamp; the rest just keep IDs unique.
Related
Extract the creation timestamp from a Discord or Twitter/X ID.
Find and convert timestamp fields anywhere in a JSON payload.
Excel serial dates, .NET ticks, GPS time, and more.