MongoDB - 使用 12 字节字符串作为唯一标识符而不是增量值的优势 [英] MongoDB - Advantage of using 12 bytes string as unique identifier instead of incremental value
问题描述
MongoDB 生成 uuid 作为唯一标识符而不是增量值作为唯一标识符是否有任何具体原因?
Is there any specific reason for MongoDB generating uuid as unique identifier instead of incremental values as unique identifier?.
推荐答案
递增值或序列需要一个中心参考点,这是缩放的限制因素.ObjectID 被设计为合理唯一的 ID,可以在分布式环境中独立生成,具有单调递增的值(领先的时间戳组件)以进行近似排序.
Incrementing values or sequences require a central point of reference which is a limiting factor for scaling. ObjectIDs are designed to be reasonably unique IDs that can be independently generated in a distributed environment with monotonically increasing values (a leading timestamp component) for approximate ordering.
ObjectIDs 通常由 MongoDB 驱动程序生成,因此无需进行服务器往返来查找下一个可用的 _id
或等待插入操作的服务器结果来了解 _id
已分配.如果驱动程序或客户端应用程序插入文档时不包含 _id
值,则 mongod
服务器将生成 ObjectID.
ObjectIDs are typically generated by MongoDB drivers so there is no need to make a server round-trip to find the next available _id
or wait for the server result of an insert operation to know what _id
was allocated. If a driver or client application inserts a document without including an _id
value, an ObjectID will be generated by the mongod
server.
在 MongoDB 中使用 ObjectID 没有严格要求:如果您的数据有更自然的唯一键或者您更喜欢替代主键格式,您可以提供自己的 _id
值.
There is no strict requirement to use ObjectIDs in MongoDB: you can provide your own _id
values if there is a more natural unique key for your data or you prefer an alternative primary key format.
这篇关于MongoDB - 使用 12 字节字符串作为唯一标识符而不是增量值的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!