Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the advantage of using an ObjectId instead of a plain String?

Tags:

mongodb

What is the advantage to using an ObjectId instead of, say, a UUID as a plain String?

like image 390
Daniel Serodio Avatar asked May 01 '12 23:05

Daniel Serodio


People also ask

What is the use of ObjectId?

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.

Is MongoDB ObjectId a string?

A MongoDB ObjectId is a 12-byte UUID can be used as a HEX string representation with 24 chars in length.

What is an ObjectId?

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.

What is the size of an ObjectId in?

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.


1 Answers

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.

like image 150
Eve Freeman Avatar answered Oct 28 '22 05:10

Eve Freeman