MongoDB 中“id”和“_id”字段的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9694460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between "id" and "_id" fields in MongoDB
提问by Arthur Neves
Is there any difference between using the field ID or _ID from a MongoDB document?
使用 MongoDB 文档中的字段 ID 或 _ID 有什么区别吗?
I am asking this, because I usually use "_id", however I saw this sort({id:-1}) in the documentation: http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Sortbyidtosortbyinsertiontime
我问这个,因为我通常使用“_id”,但是我在文档中看到了这种排序({id:-1}):http: //www.mongodb.org/display/DOCS/Optimizing+Object+IDs#优化ObjectIDs-Sortbyidtosortbyinsertiontime
EDIT
编辑
Turns out the docs were wrong.
原来文档是错误的。
采纳答案by Leonard Garvey
I expect it's just a typo in the documentation. The _id
field is primary key for every document. It's called _id
and is also accessible via id
. Attempting to use an id
key may result in a illegal ObjectId format
error.
我希望这只是文档中的一个错字。该_id
字段是每个文档的主键。它被调用_id
并且也可以通过id
. 尝试使用id
密钥可能会导致illegal ObjectId format
错误。
That section is just indicating that the automatically generated ObjectIDs start with a timestamp so it's possible to sort your documents automatically. This is pretty cool since the _id
is automatically indexed in every collection. See http://www.mongodb.org/display/DOCS/Object+IDsfor more information. Specifically under "BSON ObjectID Specification".
该部分只是表明自动生成的 ObjectID 以时间戳开头,因此可以自动对文档进行排序。这非常酷,因为它_id
会在每个集合中自动编入索引。有关更多信息,请参阅http://www.mongodb.org/display/DOCS/Object+IDs。具体在“BSON ObjectID 规范”下。
A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON.
BSON ObjectID 是一个 12 字节的值,由 4 字节的时间戳(自纪元以来的秒数)、3 字节的机器 id、2 字节的进程 id 和 3 字节的计数器组成。请注意,与 BSON 的其余部分不同,时间戳和计数器字段必须以大端存储。
回答by Bryan Migliorisi
The _id
field is the default field for Bson ObjectId's and it is,by default, indexed.
该_id
字段是 Bson ObjectId 的默认字段,默认情况下是索引的。
_id
and id are not the same. You may also choose to add a field called id
if you want, but it will not be index unless you add an index.
_id
和 id 不一样。id
如果你愿意,你也可以选择添加一个名为的字段,但除非你添加索引,否则它不会是索引。
It is just a typo in the docs.
这只是文档中的一个错字。
回答by tessie
id is an alias for _id in mongoid.id would return the _id of the document. https://github.com/mongodb/mongoid/blob/master/lib/mongoid/fields.rb#L47
id 是 mongoid.id 中 _id 的别名,将返回文档的 _id。 https://github.com/mongodb/mongoid/blob/master/lib/mongoid/fields.rb#L47
if the _id field is not specified an ObjectedId is generated automatically.
如果未指定 _id 字段,则会自动生成 ObjectedId。
回答by xameeramir
My two cents:
我的两分钱:
The _id field
_id 字段
MongoDB
assigns an _id
field to each document and assigns primary index on it. There're ways by which we can apply secondary indices as well. By default, MongoDB
creates values for the _id
field of type ObjectID
. This value is defined in BSON
spec and it's structured this way:
MongoDB
为_id
每个文档分配一个字段并为其分配主索引。我们也可以通过多种方式应用二级索引。默认情况下,为 type 字段MongoDB
创建值。这个值是在规范中定义的,它的结构是这样的:_id
ObjectID
BSON
ObjectID (12 bytes HEX string) = Date (4 bytes, a timestamp value representing number of seconds since the Unix epoch) + MAC address (3 bytes) + PID (2 bytes) + Counter (3 bytes)
ObjectID(12 字节 HEX 字符串)= 日期(4 字节,时间戳值表示自 Unix 纪元以来的秒数)+ MAC 地址(3 字节)+ PID(2 字节)+ 计数器(3 字节)