What is the advantage to using an ObjectId instead of, say, a UUID as a plain String?
MongoDB uses ObjectIds as the default value of _id field of each document, which is generated during the creation of any document. Object ID is treated as the primary key within any MongoDB collection. It is a unique identifier for each document or record.
A MongoDB ObjectId is a 12-byte UUID can be used as a HEX string representation with 24 chars in length.
An ObjectID is a unique, not null integer field used to uniquely identify rows in tables in a geodatabase. ObjectIDs are limited to 32-bit values, which store a maximum value of 2,147,483,647.
ObjectId values are 12 bytes in length, consisting of: A 4-byte timestamp, representing the ObjectId's creation, measured in seconds since the Unix epoch. A 5-byte random value generated once per process. This random value is unique to the machine and process.
An ObjectId is binary, and thus takes up less space. ObjectIds also have the sorting factor--they will end up being in insertion order (or very close), while remaining unique. The sorting can be good for some things, but unwanted for others (like shard keys). You can also extract the timestamp (second resolution) from an ObjectId, which can be convenient.
Aside from that, I would say there's not much difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With