FormatterHQ

MongoDB ObjectId Generator

Create valid MongoDB ObjectIds in bulk for the current time or a date you choose, formatted as plain hex, shell ObjectId("…") calls or Extended JSON.

Output TEXT
Ready.

How to use the MongoDB ObjectId Generator

  1. Set How many ObjectIds you need (1 to 1,000).
  2. Choose a Format: hex string, ObjectId("…") or Extended JSON {"$oid": …}.
  3. Optionally enter a Custom time such as 2024-05-01T12:00:00Z to backdate the IDs.
  4. Click Generate ObjectIds and copy the output into your fixture, script or shell.

MongoDB ObjectId generator

This ObjectId generator builds 12-byte BSON ObjectIds with the same layout the MongoDB drivers use, so the values are accepted anywhere an _id of type ObjectId is expected. You can generate them for the current second or for any date an ObjectId can hold (1970-01-01 to 2106-02-07 UTC), which makes it easy to prepare seed data, test fixtures and migration scripts with realistic creation times.

How an ObjectId is built

BytesHex charsContent
41–8Creation time in seconds since the Unix epoch (big-endian)
59–18Random value, fixed per process (here: per page load)
319–24Counter that starts at a random value and increments for each ID

Because the timestamp comes first, ObjectIds sort roughly by creation time, which is why sorting by _id in MongoDB approximates insertion order.

Example

With Custom time set to 2024-05-01T12:00:00Z and the ObjectId("…") format, you get output like:

ObjectId("66322ec095a286ddbaccf54a")
ObjectId("66322ec095a286ddbaccf54b")

The first eight hex digits, 66322ec0, are 1714564800 — exactly that date in Unix seconds. The middle part is the same for every ID from this page, and the last three bytes count up by one. Only the timestamp is predictable; your random and counter parts will differ.

Output formats

  • Hex string — 6ab7373995a286ddbaccf547, for drivers, REST payloads and CSV imports.
  • ObjectId("…") — paste straight into mongosh or a Compass query.
  • Extended JSON — { "$oid": "…" }, the form used by mongoimport, mongoexport and canonical Extended JSON output.

Tips and pitfalls

  • Second precision. The timestamp has no milliseconds; all IDs from one batch share it and are ordered by the counter.
  • Backdated IDs are still unique. The random and counter bytes differ, so generating many IDs for the same past date does not create duplicates.
  • Invalid dates. If the custom time cannot be parsed, the tool asks for a value such as 2024-05-01T12:00:00Z. Include the Z or an offset to avoid local-time surprises. A date before 1970 or after 2106-02-07 does not fit the 32-bit seconds field and is rejected with an error.

To check what an existing ID contains, use the ObjectId to timestamp decoder. The Unix timestamp converter helps when you need the seconds value for a date.

Querying by creation date with a generated ID

Because the timestamp sits in the first four bytes, you can use an ObjectId as a date boundary without storing a separate createdAt field. Generate an ID for the start date, keep the first eight hex digits and replace the rest with zeros:

// documents created on or after 2024-05-01T12:00:00Z
db.orders.find({ _id: { $gte: ObjectId("66322ec00000000000000000") } })

The zeroed tail is the smallest possible value for that second, so the query includes every document created from then on. This works well for quick checks and exports. For reports that need exact times or time zones, a real date field is still the better choice, since the ObjectId only records whole seconds from the client or driver that created it.

Privacy

IDs are generated in your browser with crypto.getRandomValues for the random and counter bytes. No request is made to a server or a database.

Frequently asked questions

Are these ObjectIds valid in MongoDB?
Yes. They are 24 hexadecimal characters encoding the standard 12-byte layout, so MongoDB, mongosh and every official driver accept them as ObjectId values.
Can I create an ObjectId for a past date?
Yes. Enter an ISO date in the Custom time field and every generated ID will carry that creation time. This is useful for seed data or for building range queries on _id.
Will generated ObjectIds collide with ones in my database?
It is very unlikely. The 5 random bytes and the randomly seeded counter make each ID distinct, just as they do in the drivers, even when the timestamp matches existing documents.
What is the difference between ObjectId and a UUID?
An ObjectId is 12 bytes and always contains a creation time in seconds, while a UUID is 16 bytes and only some versions contain a time. MongoDB uses ObjectId as the default _id type, but you can store UUIDs as binary subtype 4 if you prefer.

Last updated October 3, 2026

Related tools

MongoDB ObjectId to Timestamp Extract the creation date from MongoDB ObjectIds and see the random and counter parts; also builds a date-range _id query. Password Generator Generate strong random passwords with custom length and character sets, using a cryptographically secure source. Random Alphanumeric Generator Generate letters-and-digits codes for vouchers, reference IDs and usernames in mixed, upper or lower case. Random Binary Generator Generate random binary numbers (strings of 0s and 1s) of any bit length. Random Byte Generator Generate cryptographically secure random bytes as hex, Base64, decimal, binary or a code array. Random Color Generator Generate random colours as HEX, RGB or HSL codes with a swatch preview for inspiration and testing. Random CSV Generator Generate realistic fake CSV test data — names, emails, addresses, dates and more — with custom fields. Random Data from Regex Generate random strings that match a regular expression — ideal for test data such as IDs, codes and formats. Random Date Generator Generate random dates or date-times between two dates in ISO, US, EU, long, Unix or RFC formats. Random Decimal Generator Generate random decimal (floating-point) numbers in a range with a fixed number of decimal places. Random Emoji Generator Generate random emoji from smileys, animals, food, travel, objects or symbols to copy and paste. Random Fraction Generator Generate random proper fractions such as 3/7 with a maximum denominator, simplified automatically.