将 _Id 作为对象或字符串存储在 MongoDB 中?

发布于 2024-12-09 16:37:45 字数 215 浏览 1 评论 0原文

我正在使用 Codeigniter 和 MongoDB 开发 API。 在此系统中,我保存所选用户的全名和_ID 正在跟随。

关于 _Id 最好做什么?将其存储为对象还是字符串? 如果我将其存储为对象,则在回显追随者时需要将其转换为字符串,否则 输出看起来很奇怪。

我的问题是真的。可以将 _Id 存储为字符串而不是对象吗? 存储为字符串有什么缺点?

感谢所有的投入!

I am developing an API using Codeigniter and MongoDB.
In this system I am saving the full name and _ID of users that the selected user
is following.

What is best to do regarding the _Id? Store it as an object or as a string?
If I store it as an object I need to convert it to string when echoing out followers otherwise
the output looks strange.

My question is really. Is it ok to store the _Id as a string rather than an object?
What is the downside of storing as string?

Thankful for all input!

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

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

发布评论

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

评论(2

生来就爱笑 2024-12-16 16:37:45

使用 objectid 时,请求(和更新)的性能确实更好。而且,objectid 的空间非常小。

来自官方文档:

BSON 包括用于存储字节数组的二进制数据类型。使用
这将使 id 值及其各自的键在 _id 中
索引,小两倍。

这里有 2 个可以帮助您的链接:
- http://www.mongodb.org/display/DOCS/Optimizing+Object+ ID
- http://www.mongodb.org/display/DOCS/Object+IDs

Performance for requests (and updates) are really better with objectid. More over, objectid are quite small in space.

From the official doc :

BSON includes a binary data datatype for storing byte arrays. Using
this will make the id values, and their respective keys in the _id
index, twice as small.

here are 2 links that can help you :
- http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs
- http://www.mongodb.org/display/DOCS/Object+IDs

乖不如嘢 2024-12-16 16:37:45

当您使用 ObjectId 时,它会生成 _id 作为所有计算机中的唯一值。所以如果你使用Sharding,你就不会担心_id冲突。查看 规范 中 ObjectId 是如何生成的

但如果你使用字符串,你应该小心地生成它。

When you use ObjectId, it generates _id as a unique value in all your computers. So if you use Sharding, you will not worry about you _id conflicts. See how ObjectId generates in specification

But if you use string, you should generate it carefully.

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