MongoDB 如何在一个集合中排序他们的文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33018048/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:33:27  来源:igfitidea点击:

How does MongoDB order their docs in one collection?

mongodbsortingmongodb-query

提问by Paola G

In my Usercollection, MongoDB usually orders each new doc in the same order I create them: the last one created is the last one in the collection. But I have detected another collection where the last one I created has the 6 position between 27 docs.

在我的User集合中,MongoDB 通常按照我创建它们的相同顺序对每个新文档进行排序:创建的最后一个是集合中的最后一个。但是我检测到另一个集合,其中我创建的最后一个集合在 27 个文档之间有 6 个位置。

Why is that?

这是为什么?

Which order follows each doc in MongoDB collection?

MongoDB 集合中的每个文档遵循哪个顺序?

回答by Ionic? Biz?u

It's called natural order:

它被称为自然顺序

natural order

The order in which the database refers to documents on disk. This is the default sort order. See $naturaland Return in Natural Order.

natural order

数据库引用磁盘上文档的顺序。这是默认的排序顺序。见$naturalReturn in Natural Order

This confirms that in general you get them in the same order you inserted, but that's not guaranteed–as you noticed.

这证实了通常您会按照插入的相同顺序获取它们,但这并不能保证——正如您所注意到的。

Return in Natural Order

The $naturalparameter returns items according to their natural orderwithin the database. This ordering is an internal implementation feature, and you should not rely on any particular structure within it.

Index Use

Queries that include a sort by $naturalorder do notuse indexes to fulfill the query predicate with the following exception: If the query predicate is an equality condition on the _idfield { _id: <value> }, then the query with the sort by $naturalorder can use the _idindex.

MMAPv1

Typically, the natural order reflects insertion order with the following exception for the MMAPv1 storage engine. For the MMAPv1 storage engine, the natural order does not reflect insertion order if the documents relocate because of document growthor remove operations free up space which are then taken up by newly inserted documents.

按自然顺序返回

$natural参数根据项目在数据库中的自然顺序返回项目。此排序是内部实现功能,您不应依赖其中的任何特定结构。

索引使用

包含按$natural顺序排序的查询使用索引来满足查询谓词,但以下情况除外:如果查询谓词是_id字段上的相等条件{ _id: <value> },则按$natural顺序排序的查询可以使用_id索引。

MMAPv1

通常,自然顺序反映插入顺序,但 MMAPv1 存储引擎除外。对于 MMAPv1 存储引擎,如果文档由于文档增长而重新定位或删除操作释放空间,然后由新插入的文档占用空间,则自然顺序不会反映插入顺序。

Obviously, like the docs mentioned, you should notrely on this default order (This ordering is an internal implementation feature, and you should not rely on any particular structure within it.).

很显然,像文档提到的,你应该依靠此默认顺序(此排序是内部实现的功能,你不应该依赖于其中的任何特定结构。)。

If you need to sort the things, use the sort solutions.

如果您需要对事物进行排序,请使用排序解决方案



Basically, the following two calls should return documents in the same order (since the default order is $natural):

基本上,以下两个调用应该以相同的顺序返回文档(因为默认顺序是$natural):

db.mycollection.find().sort({ "$natural": 1 })
db.mycollection.find()

If you want to sort by another field (e.g. name) you can do that:

如果你想按另一个字段(例如name)排序,你可以这样做:

db.mycollection.find().sort({ "name": 1 })

回答by Blakes Seven

MongoDB does not "order" the documents at all, unless you ask it to.

除非您要求,否则 MongoDB 根本不会“订购”文档。

The basic insertion will create an ObjectIdin the _idprimary key value unless you tell it to do otherwise. This ObjectIdvalue is a special value with "monotonic"or "ever increasing" properties, which means each value created is guaranteed to be larger than the last.

基本插入将ObjectId_id主键值中创建一个,除非您告诉它不这样做。这个ObjectId值是一个具有“单调”或“不断增加”属性的特殊值,这意味着创建的每个值都保证大于最后一个。

If you want "sorted" then do an explicit "sort":

如果你想要“排序”,那么做一个明确的“排序”:

 db.collection.find().sort({ "_id": 1 })

Or a "natural"sort means in the order stored on disk:

或者“自然”排序意味着按存储在磁盘上的顺序:

db.collection.find().sort({ "$natural": 1 })

Which is pretty much the standard unless stated otherwise or an "index" is selected by the query criteria that will determine the sort order. But you can use that to "force" that order if query criteria selected an index that sorted otherwise.

除非另有说明,否则这几乎是标准,或者由将确定排序顺序的查询条件选择的“索引”。但是,如果查询条件选择了以其他方式排序的索引,则可以使用它来“强制”该顺序。

MongoDB documents "move" when grown, and therefore the _idorder is not always explicitly the same order as documents are retrieved.

MongoDB 文档在增长时会“移动”,因此_id顺序并不总是与检索文档的顺序明确相同。

回答by Philipp

For performance reasons, MongoDB never splits a document on the hard drive.

出于性能原因,MongoDB 从不拆分硬盘驱动器上的文档。

When you start with an empty collection and start inserting document after document into it, mongoDB will place them consecutively on the disk.

当您从一个空集合开始并开始将一个又一个文档插入其中时,mongoDB 会将它们连续放置在磁盘上。

But what happens when you update a document and it now takes more space and doesn't fit into its old position anymore without overlapping the next? In that case MongoDB will delete it and re-append it as a new one at the end of the collection file.

但是当你更新一个文档并且它现在占用更多空间并且不再适合它的旧位置而不与下一个重叠时会发生什么?在这种情况下,MongoDB 将删除它并在集合文件的末尾重新附加它作为一个新的。

Your collection file now has a hole of unused space. This is quite a waste, isn't it? That's why the next document which is inserted and small enough to fit into that hole will be inserted in that hole. That's likely what happened in the case of your second collection.

您的收藏文件现在有一个未使用的空间。这很浪费,不是吗?这就是为什么下一个插入且足够小以适合该孔的文档将插入该孔的原因。这很可能是您的第二个收藏中发生的情况。

Bottom line:Never rely on documents being returned in insertion order. When you care about the order, always sort your results.

底线:永远不要依赖按插入顺序返回的文档。当您关心顺序时,请始终对结果进行排序。

回答by Paola G

I could find out more about it thanks to the link Return in Natural Orderprovided by Ionic? Biz?u.

感谢Ionic提供的按自然顺序返回的链接,我可以找到更多相关信息?Biz?你

"The $natural parameter returns items according to their natural order within the database.This ordering is an internal implementation feature, and you should not rely on any particular structure within it.

“$natural 参数根据它们在数据库中的自然顺序返回项目。此排序是一个内部实现功能,您不应依赖其中的任何特定结构。

Typically, the natural order reflects insertion order with the following exception for the MMAPv1 storage engine. For the MMAPv1 storage engine, the natural order does not reflect insertion order if the documents relocate because of document growth or remove operations free up space which are then taken up by newly inserted documents."

通常,自然顺序反映插入顺序,但 MMAPv1 存储引擎除外。对于 MMAPv1 存储引擎,如果文档因文档增长或删除操作释放空间而被新插入的文档占用而重新定位,则自然顺序不会反映插入顺序。”