MongoDB ObjectID Generator

MongoDB ObjectID Generator
Count
ObjectID Decoder
ObjectID Structure

MongoDB ObjectIDs are 12-byte identifiers consisting of:

  • Timestamp: 4 bytes (creation time)
  • Random Value: 5 bytes (unique per machine/process)
  • Counter: 3 bytes (incremental)

Total: 24 hexadecimal characters

Use Cases
  • MongoDB Documents - Primary key generation
  • API Resources - Unique resource identifiers
  • Distributed Systems - Cross-service references
  • Data Migration - Preserving document relationships
  • Testing - Mock data generation
ObjectID Properties
  • Globally Unique
  • Timestamp Embedded
  • Sortable by Creation
  • No Central Coordination
  • URL Safe

Inside a MongoDB ObjectID

An ObjectID is 12 bytes: a 4-byte Unix timestamp, a 5-byte random value fixed per process, and a 3-byte incrementing counter. That layout means ObjectIDs sort roughly by creation time and can be generated on any client without coordination - and it also means anyone holding an ID can read the creation time straight out of it. The full byte layout and its consequences are unpacked in MongoDB ObjectID explained.

Generate valid ObjectIDs here for seeding test data or building API fixtures. If you are choosing an ID format for a system that is not MongoDB, compare UUID and ULID first - both are 16 bytes and standardised.

ObjectID Questions

MongoDB's default primary key - 12 bytes containing a timestamp, random value, and counter. Globally unique without coordination, and you can extract when it was created.

Yes, the first 4 bytes are a Unix timestamp. Paste any ObjectID here and the decoder will show you when it was created. No need for a separate createdAt field.

For MongoDB, stick with ObjectID. It's smaller (12 vs 16 bytes), built-in, and time-sortable. Only use UUID if you need cross-system compatibility.

Roughly. Same second + same machine = sequential. Different machines or different seconds = generally ordered but not strictly sequential.