Omkar Kulkarni
Omkar Kulkarni

Reputation: 1215

Why does mongodb has ObjectID as string?

As a general understanding, numeric operations are faster than string operations. It is faster to compute 2 < 5 than "hello" < "world".
Also, since the length of the _id is 12. If it is stored as a string where a character can take 2 bytes and thus the entire string would take 24 bytes. However a number of 8 bytes can very easily store this value of 12 digits. Basically, a number type would take lesser space.
So with these logics, why does mongodb store _id as string type ObjectIDs and not numeric ObjectID?

Upvotes: 0

Views: 102

Answers (1)

Gibbs
Gibbs

Reputation: 22974

Your understanding is incorrect.

It is much better than Strings. It just takes 12 bytes in memory as mentioned in the documentation

Java implementation for the same.

Refer toHexString in the github link, that's what returned to you when you ask for String. It actually takes 24bytes whereas ObjectId takes only 12bytes.

Upvotes: 1

Related Questions