这里是这个问题的具体原因:
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/