Daniel Serodio
Daniel Serodio

Reputation: 4516

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

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

Upvotes: 13

Views: 1985

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33185

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.

Upvotes: 18

Related Questions