WiredTiger MongoDB 引擎排序:“自然顺序”是否等同于 "ordered" WiredTiger 引擎在 mongodb 中?
WiredTiger MongoDB engine ordering : Is" natural order" equivalent to "ordered" with WiredTiger engine in mongodb?
这里是这个问题的具体原因:
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 一起使用是否总是 return 第一个符合搜索条件的索引文档(具有最低 _id 索引的文档)。
-> 然后 findOne() 将等同于 "findFirst() according to _id" ,并且将保证排序:正确吗?
谢谢,
朱利安
自然表示按插入顺序,而不是按id顺序。
The doc 并没有说它不适用于 WiredTiger,所以我假设它也适用于这个存储引擎:
Use the $natural operator to use natural order for the results of a sort operation. Natural order refers to the logical ordering of documents internally within the database.
与其担心究竟是什么定义了自然秩序,不如将其视为 "whatever the database wants to do at that instant"。您永远不应该依赖自然顺序,因为您几乎无法保证 return 文档的运行方式,尤其是在比较存储引擎时。这不是基于插入时间的顺序,尽管它可能是;它不是基于磁盘位置的顺序,尽管它可能是;它不是基于 _id
的订单,尽管它可能是。它只是 return 的文档,但是实施者根据没有请求特定的顺序来决定是最好的还是最简单的,因此它被称为 "natural"。由于它是一个实现细节,它可能会随时更改。
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
索引。
这里是这个问题的具体原因:
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 一起使用是否总是 return 第一个符合搜索条件的索引文档(具有最低 _id 索引的文档)。
-> 然后 findOne() 将等同于 "findFirst() according to _id" ,并且将保证排序:正确吗?
谢谢,
朱利安
自然表示按插入顺序,而不是按id顺序。 The doc 并没有说它不适用于 WiredTiger,所以我假设它也适用于这个存储引擎:
Use the $natural operator to use natural order for the results of a sort operation. Natural order refers to the logical ordering of documents internally within the database.
与其担心究竟是什么定义了自然秩序,不如将其视为 "whatever the database wants to do at that instant"。您永远不应该依赖自然顺序,因为您几乎无法保证 return 文档的运行方式,尤其是在比较存储引擎时。这不是基于插入时间的顺序,尽管它可能是;它不是基于磁盘位置的顺序,尽管它可能是;它不是基于 _id
的订单,尽管它可能是。它只是 return 的文档,但是实施者根据没有请求特定的顺序来决定是最好的还是最简单的,因此它被称为 "natural"。由于它是一个实现细节,它可能会随时更改。
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
索引。