What is MongoDB ObjectID?
ObjectID Fundamentals
MongoDB ObjectID is a 12-byte unique identifier that serves as the default primary key for documents in MongoDB databases. This powerful identifier not only ensures global uniqueness but also embeds timestamp information, allowing developers to easily track document creation times.
- •12-byte structure: Timestamp (4 bytes) + Random value (5 bytes) + Counter (3 bytes)
- •Global uniqueness: Ensures identifier uniqueness across distributed systems
- •Embedded timestamp: Document creation time can be extracted directly from ObjectID
- •Hexadecimal representation: Displayed as 24-character hexadecimal string
Use Cases
- •MongoDB Development: Generate unique primary key identifiers for new documents
- •Data Migration: Create test data during database migration processes
- •API Development: Generate unique resource identifiers for RESTful APIs
- •Temporal Analysis: Extract and analyze creation timestamps from existing ObjectIDs