为什么MongoDB将Objectid作为字符串?

发布于 2025-02-08 08:15:14 字数 237 浏览 0 评论 0 原文

总体上,数字操作比字符串操作快。计算 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2025-02-15 08:15:14

您的理解是不正确的。

它比字符串好得多。如

。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文