在 MongoDB 中创建简短、唯一的对象 ID
我正在使用 Rails/Mongoid 制作一个类似于 Instagram 的应用程序。我想要一个可以在 http://instagr.am/p/DJmU8/< 等网址中使用的唯一 ID /a>
最简单的方法是什么?我可以从 Mongo 创建的默认 BSON ObjectID 中派生出这样的 ID 吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试使用 ObjectID 的前 4 个字节(它们将代表时间戳)。
但是,为了 100% 安全,最好通过实施计数器来生成真正唯一的短 ID。您可以使用单独的集合来维护计数器的当前值。
有关 mongo 的 ObjectID 结构的更多详细信息,请参见:http://www.mongodb.org/ display/DOCS/Object+IDs
作为替代方案,您可以将十六进制字符串 id 表示形式转换为基于 36 个符号(26 个拉丁字母 + 10 个数字)的表示形式。显然会更短。
似乎有一个 ruby 库可以进行此类转换 http://rubyworks.github.com/radix /
You may try to use first 4 bytes of ObjectID (they will represent timestamp).
But, to be 100% safe, it's better to produce really unique short id, by implementing a counter. You can use separate collection to maintain current value of your counter.
More details on mongo's ObjectID structure can be found here: http://www.mongodb.org/display/DOCS/Object+IDs
As an alternative you can convert convert hex string id representation to a representation based on 36 symbols (26 latin letters + 10 digits). It will obviously be shorter.
It seems, that there is a ruby library, that can do such conversions http://rubyworks.github.com/radix/
为什么不使用 dylang/shortid ?
使用 npm npmjs.com/package/shortid 安装:
然后 require:
在 mongoose schema 中:
或者只是直接插入:
Why not use dylang/shortid?
Install using npm npmjs.com/package/shortid:
Then require:
In mongoose schema:
or just insert directly:
您可以尝试 Mongoid::Token
https://github.com/thetron/mongoid_token
从文档中:
You could try Mongoid::Token
https://github.com/thetron/mongoid_token
From the docs:
@aav 提到您可以使用前 4 个字节,但该值以秒为单位,您甚至可以每秒插入 10.000 或更多。另外,objectID 是 Uniq,您需要检查“何时”从重复值“Write Concerns”中收到错误?
此代码看起来很有趣:
https: //github.com/treygriffith/short-mongo-id/blob/master/lib/objectIdToShortId.js
另请检查 ObjectID 时间戳解析器:
https://steveridout.github.io/mongo-object-time/
或者您可以执行 ObjectId().toString() 并通过 hashids [nodejs、php 等等]
bson 然后通过 hids
输出:
如果有以下情况,您可能会发生冲突:更多进程正在运行,然后考虑将 PID 添加到唯一的并且当您在不同计算机上运行多个实例时
@aav was mention that you can use first 4 bytes, but this value are in seconds and you can get even 10.000 or more insert per seconds. Other thing objectID is Uniq and you need check "when" you get error from duplicate value "Write Concerns"?
This code look interesting:
https://github.com/treygriffith/short-mongo-id/blob/master/lib/objectIdToShortId.js
Check also ObjectID timestamp parser:
https://steveridout.github.io/mongo-object-time/
Or you can execute ObjectId().toString() and base of this string create new by hashids [nodejs,php, andmanymore]
Output:
You can get collision if: more process are running, then consider add PID to unique and when you run multiple instance on different computer
尝试gem https://github.com/jffjs/mongoid_auto_inc
Try gem https://github.com/jffjs/mongoid_auto_inc
Hashids 库旨在生成这样的 ID。在这里查看 ☞ https://github.com/peterhellberg/hashids.rb
The Hashids library is meant for generating IDs like this. Check it out here ☞ https://github.com/peterhellberg/hashids.rb