总体上,数字操作比字符串操作快。计算 2< 5
比“ hello”< “世界”
。
同样,由于_id的长度为12。如果将其存储为字符串,则角色可以在其中取2个字节,因此整个字符串将占24个字节。但是,许多8个字节很容易存储此值12位。基本上,数字类型会占用较小的空间。
因此,使用这些逻辑,为什么MongoDB将_id存储为字符串类型Objectids而不是数字ObjectID?
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?
发布评论
评论(1)
您的理解是不正确的。
它比字符串好得多。如
。 a href =“ https://github.com/mongodb/mongo-java-driver/blob/master/master/bbson/src/src/main/org/org/bbson/types/object/obigntid.java, /a>相同。
在GitHub链接中,当您要求字符串时,这就是返回给您的原因。实际上,它需要24个比例,而objectid仅需12 bytes。
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.