How to generate a MongoDB ObjectId in Node.js
If you are writing backend code, you don't need an external library. You can generate a standard MongoDB ObjectId in Node.js directly using the native driver: const { ObjectId } = require('mongodb'); const id = new ObjectId();. However, if you need to quickly generate IDs without firing up a Node script, or you need a Bulk MongoDB ObjectId generator for testing your database endpoints, this browser-based tool allows you to mock thousands of valid BSON ObjectIDs instantly.
Decode MongoDB ObjectId timestamp online
Unlike standard random UUIDs, an ObjectId intrinsically stores the exact second it was generated. This means you do not need a separate created_at column in your database; you can parse the date directly from the ID.
Follow these steps to extract the creation date:
- Paste your 24-character hexadecimal ObjectID into the Decoder input box.
- Click 'Decode'. The tool will split the string and extract the first 8 characters (4 bytes).
- The tool converts those 8 hex characters into a Unix timestamp to reveal the exact creation date.
Our visual parser above demonstrates the full 12-byte breakdown:
- Bytes 0-3 (8 Hex Chars): A Unix timestamp (in seconds). This allows MongoDB to sort records purely by their ObjectID without requiring a secondary index.
- Bytes 4-8 (10 Hex Chars): A random value generated once per process. Historically, this used to be a combination of the machine's MAC address and process ID (PID), but was changed to a pure random value in MongoDB v3.4 for security reasons.
- Bytes 9-11 (6 Hex Chars): A 3-byte incrementing counter, initialized to a random value. If multiple IDs are generated within the exact same second by the same process, this counter guarantees they remain unique.
Generate BSON ObjectId for unit testing
Yes. Database engineers often need to generate mock data or query documents created before or after a specific date. You can use our bulk MongoDB ObjectId generator for testing by supplying a custom timestamp in the "Bulk Generator" tab. The tool will generate valid ObjectIDs starting exactly at that second, which can then be safely used in your unit tests or MongoDB queries like { _id: { $gt: ObjectId("YOUR_GENERATED_ID") } }.