MongoDB中集合ID的长度 [英] Collection ID length in MongoDB
问题描述
我是mongodb的新手,并且堆栈溢出.
i am new to mongodb and stack overflow.
我想知道为什么在mongodb上ID为24个十六进制字符? 这有什么重要性?
I want to know why on mongodb collection ID is of 24 hex characters? what is importance of that?
推荐答案
为什么默认的_id
是24个字符的十六进制字符串?
作为MongoDB文档的主键(_id
)生成的默认唯一标识符是 ObjectId .这是一个12字节的二进制值,通常表示为24个字符的十六进制字符串,并且是MongoDB支持的标准字段类型之一 BSON规范.
Why is the default _id
a 24 character hex string?
The default unique identifier generated as the primary key (_id
) for a MongoDB document is an ObjectId. This is a 12 byte binary value which is often represented as a 24 character hex string, and one of the standard field types supported by the MongoDB BSON specification.
ObjectId的12个字节的构造方法是:
The 12 bytes of an ObjectId are constructed using:
- 一个4字节的值,表示自Unix时代以来的秒数
- 3字节机器标识符
- 一个2字节的进程ID
- 3字节计数器(以随机值开头)
ObjectId(或根据 GUID 公式生成的类似标识符)允许在分布式系统中独立生成唯一标识符
ObjectIds (or similar identifiers generated according to a GUID formula) allow unique identifiers to be independently generated in a distributed system.
The ability to independently generate a unique ID becomes very important as you scale up to multiple application servers (or perhaps multiple database nodes in a sharded cluster). You do not want to have a central coordination bottleneck like a sequence counter (eg. as you might have for an auto-incrementing primary key), and you will want to insert new documents without risk that a new identifier will turn out to be a duplicate.
ObjectId通常是由您的MongoDB 客户端驱动程序生成的,但如果您的MongoDB服务器客户端驱动程序或应用程序代码,或者尚未添加_id
字段.
An ObjectId is typically generated by your MongoDB client driver, but can also be generated on the MongoDB server if your client driver or application code or haven't already added an _id
field.
不.如果您有更适合使用的唯一标识符,则始终可以为_id
提供自己的值.它可以是单个值,也可以是使用多个字段的复合值.
No. If you have a more suitable unique identifier to use, you can always provide your own value for _id
. This can either be a single value or a composite value using multiple fields.
_id
值的主要限制是它们对于集合必须是唯一的,并且您不能更新或删除现有文档的_id
.
The main constraints on _id
values are that they have to be unique for a collection and you cannot update or remove the _id
for an existing document.
这篇关于MongoDB中集合ID的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!