将 _Id 作为对象或字符串存储在 MongoDB 中?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 objectid 时,请求(和更新)的性能确实更好。而且,objectid 的空间非常小。
来自官方文档:
这里有 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 :
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
当您使用
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 specificationBut if you use string, you should generate it carefully.