mongodb - WiredTiger MongoDB 引擎排序 : Is"natural order"equivalent to "ordered" with WiredTiger engine in mongodb?

标签 mongodb wiredtiger

这里是这个问题的具体原因:

db.collection.findOne() 函数记录为:

"If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk".

根据 http://docs.mongodb.org/manual/reference/method/cursor.sort/#mmapv1这种自然顺序的概念似乎只适用于 MMAPv1 而不适用于 wiredTiger。

然后,我想知道将 db.collection.findOne() 与 wiredTiger 一起使用是否会始终返回与搜索条件匹配的第一个索引文档(具有最低 _id 索引的文档)。

-> 那么 findOne() 将等同于“根据 _id 的 findFirst()”,并且将保证排序:对吗?

谢谢,

朱利安

最佳答案

与其担心究竟是什么定义了自然顺序,不如将其视为“数据库在那一瞬间想要做的任何事情”。您永远不应该依赖自然顺序,因为您几乎无法保证它将如何返回文档,尤其是在比较存储引擎时。这不是基于插入时间的顺序,尽管它可能是;它不是基于磁盘位置的顺序,尽管它可能是;它不是基于 _id 的订单,尽管它可能是。它只是返回文档,但是实现者根据没有请求特定的顺序来决定是最好的还是最简单的,因此它被称为“自然”。由于它是一个实现细节,因此随时可能发生变化。

I would like to know if using db.collection.findOne() with wiredTiger will always return the first indexed document (the one with the lowest _id index) that matches the search criteria

没有。如果您想要最小的 _id,请显式地对 _id 进行升序排序并取第一个结果。为此,您始终拥有 _id 索引。

关于mongodb - WiredTiger MongoDB 引擎排序 : Is"natural order"equivalent to "ordered" with WiredTiger engine in mongodb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29489551/

相关文章:

Mongodb查询从多个键中选择记录

mongodb - WiredTiger - 辅助副本集成员重新同步期间的 “too many open files error”

蒙戈数据库 4.4。副本集成员在启动时卡住(WT_VERB_RECOVERY 步骤)

mongodb - 真实集合的名称与文件系统中的名称之间的关系是什么

javascript - socket.io 将套接字与登录用户链接

javascript - 无法遍历数组并保存到 Mongoose 。回调问题?

java - 聚合以合并集合尝试

node.js - 将查询条件传递给生成查询字符串的 Node.js/Mongodb 中的 db.collection.find

MongoDB 3.2 有线老虎的性能优于 MongoDB 3.4 有线老虎