"id"与"id"之间的区别和"_id" MongoDB中的字段 [英] Difference between "id" and "_id" fields in MongoDB

查看:1102
本文介绍了"id"与"id"之间的区别和"_id" MongoDB中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MongoDB文档中的字段ID或_ID有什么区别吗?

Is there any difference between using the field ID or _ID from a MongoDB document?

我问这个问题是因为我通常使用"_id",但是我在文档中看到了这种排序方式({id:-1}):

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

编辑

结果证明文档是错误的.

Turns out the docs were wrong.

推荐答案

我希望它只是文档中的错字. _id字段是每个文档的主键.它称为_id,也可以通过id访问.尝试使用id键可能会导致illegal ObjectId format错误.

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.

该部分仅表示自动生成的ObjectID以时间戳开头,因此可以自动对文档进行排序.这很酷,因为_id会在每个集合中自动索引.参见 http://www.mongodb.org/display/DOCS/Object+IDs有关更多信息.具体在"BSON ObjectID规范"下.

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+IDs for more information. Specifically under "BSON ObjectID Specification".

BSON ObjectID是一个12字节的值,由4字节的时间戳(自纪元以来的秒数),3字节的机器ID,2字节的进程ID和3字节的计数器组成.请注意,与BSON的其余部分不同,时间戳记和计数器字段必须以大字节序存储.

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.

这篇关于"id"与"id"之间的区别和"_id" MongoDB中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆