How to Inspect MongoDB ObjectIds
ObjectId Inspection
When to Inspect ObjectIds
Decode ObjectIds when debugging MongoDB documents, estimating when a record was created without querying the database, tracing data migrations, or verifying that an ID string is a valid 24-character hex ObjectId.
Real-World Examples
507f1f77bcf86cd799439011 → created around October 2012 from embedded timestamp
Shell format: ObjectId("507f1f77bcf86cd799439011")
Compare creation order of two documents by comparing ObjectId strings lexicographically (same second)
Common Mistakes
- Confusing ObjectIds (24 hex chars) with UUIDs (32 hex + hyphens)
- Assuming the timestamp is timezone-aware — ObjectId timestamps are UTC Unix seconds
- Expecting machine and counter bytes to be meaningful in modern MongoDB deployments (often random)
Edge Cases
- ObjectIds generated in the same second on the same process increment the counter
- Manual ObjectIds can have arbitrary timestamps — do not rely on timestamp for security
- MongoDB 5.0+ may use different random byte generation — machine field is not a real MAC address
Developer Tips
- MongoDB shell:
ObjectId("...").getTimestamp() - Query by creation time range using timestamp prefix in aggregation pipelines
- Use BSON ObjectId type in drivers — avoid storing as plain strings when possible
Frequently asked questions
How is the ObjectId timestamp extracted?
The first 4 bytes (8 hex characters) of the ObjectId encode a Unix timestamp in seconds. The tool converts this to an ISO 8601 UTC datetime.
What are the machine and counter fields?
Bytes 4–8 (5 bytes) are a random value unique to the machine/process at generation time. Bytes 9–11 (3 bytes) are a counter incremented for ObjectIds created on the same machine in the same second.
Can I paste ObjectId("...") syntax?
Yes. The inspector accepts plain 24-character hex strings and MongoDB shell syntax like ObjectId("507f1f77bcf86cd799439011").