MongoDB ObjectId is a commonly used unique identifier type in MongoDB. It consists of 12 bytes and is usually represented as a 24-character hexadecimal string. ObjectId can be used as the default identifier for MongoDB documents and contains timestamp information.
The ObjectId structure contains a 4-byte timestamp, a 5-byte random value and a 3-byte incrementing counter, providing strong uniqueness across normal application scenarios.
A standard ObjectId contains 12 bytes and is normally displayed as 24 hexadecimal characters.
ObjectId contains 12 bytes internally. Each byte can be represented by two hexadecimal characters, resulting in the common 24-character representation.
The 24-character length refers to the string representation, not the actual binary size. The underlying ObjectId occupies 12 bytes.
The first 4 bytes contain a Unix timestamp associated with ObjectId generation. This allows the corresponding time to be extracted from an ObjectId.
The timestamp is useful for identification and rough time ordering, but it should not replace a dedicated creation-time field when precise application timestamps are required.
Generating ObjectIds for a specified time is useful for development, test data construction and validating MongoDB queries involving time ranges or ObjectId ordering.
ObjectId provides a high level of uniqueness for normal MongoDB usage, but it should not be interpreted as an absolute mathematical guarantee of uniqueness. Its generation depends on timestamp, random data and counter components.
Comments 0