简体   繁体   English

Model.find({}) 的默认排序行为是什么?

[英]What is the default ordering behavior for Model.find({})?

I am using this function我正在使用这个 function

Model.find({})

to return all Articles in a collection...返回集合中的所有文章...

What is the default ordering in which the articles are returned...退回文章的默认顺序是什么...

I looked at the official API for Model.find() but could not find the syntax above which I am using...我查看了Model.find()官方 API但找不到我正在使用的上述语法...

Not sure if relevant but schema is:不确定是否相关,但架构是:

schema.Article = new Schema({ 
  link:       { type: String, required: true  },
  image:      { type: String, required: true  },
  title:      { type: String, required: true  },
  summary:    { type: String, required: true  },
  tag:        { type: String, required: true, default: "health" },
  domain:     { type: String, required: true }, 
  timestamp:  { type: Date,   required: true, default: Date.now },
  owner:      { type: String, required: true, default: '5eebf1dc9148400351a49dd0' }
});

What is the default sort order when none is specified?未指定时,默认排序顺序是什么?

The default internal sort order (or natural order ) is an undefined implementation detail.默认的内部排序顺序(或自然顺序)是未定义的实现细节。 Maintaining order is extra overhead for storage engines and MongoDB's API does not mandate predictability outside of an explicit sort() or the special case of fixed-sized capped collections which have associated usage restrictions.维护顺序是存储引擎的额外开销,MongoDB 的 API 不要求在显式sort()或具有相关使用限制的固定大小上限 collections的特殊情况之外的可预测性。 For typical workloads it is desirable for the storage engine to try to reuse available preallocated space and make decisions about how to most efficiently store data on disk and in memory.对于典型的工作负载,存储引擎需要尝试重用可用的预分配空间,并决定如何最有效地将数据存储在磁盘和 memory 中。

Without any query criteria, results will be returned by the storage engine in natural order (aka in the order they are found ).如果没有任何查询条件,存储引擎将按自然顺序(也就是找到它们的顺序)返回结果。 Result order may coincide with insertion order but this behaviour is not guaranteed and cannot be relied on (aside from capped collections).结果顺序可能与插入顺序一致,但不能保证并且不能依赖此行为(除了上限集合)。

Some examples that may affect storage (natural) order:一些可能影响存储(自然)顺序的示例:

  • WiredTiger uses a different representation of documents on disk WiredTiger 在磁盘上使用不同的文档表示
    versus the in-memory cache, so natural ordering may change based on与内存缓存相比,因此自然排序可能会根据
    internal data structures.内部数据结构。

  • The original MMAPv1 storage engine (removed in MongoDB 4.2) allocates record space for documents based on padding rules.原始 MMAPv1 存储引擎(在 MongoDB 4.2 中删除)根据填充规则为文档分配记录空间。 If a document outgrows the currently allocated record space, the document location (and natural ordering) will be affected.如果文档超出当前分配的记录空间,则文档位置(和自然顺序)将受到影响。 New documents can also be inserted in storage marked available for reuse due to deleted or moved documents.由于删除或移动的文档,新文档也可以插入标记为可重复使用的存储中。

  • Replication uses an idempotent oplog format to apply write operations consistently across replica set members.复制使用幂等 oplog格式在副本集成员之间一致地应用写操作。 Each replica set member maintains local data files that can vary in natural order, but will have the same data outcome when oplog updates are applied.每个副本集成员都维护本地数据文件,这些文件可以按自然顺序变化,但在应用 oplog 更新时将具有相同的数据结果。

What if an index is used?如果使用索引怎么办?

If an index is used, documents will be returned in the order they are found (which does necessarily match insertion order or I/O order).如果使用索引,文档将按照它们被发现的顺序返回(它必须匹配插入顺序或 I/O 顺序)。 If more than one index is used then the order depends internally on which index first identified the document during the de-duplication process.如果使用了多个索引,则顺序在内部取决于在重复数据删除过程中首先标识文档的索引。

If you want a predictable sort order you must include an explicit sort() with your query and have unique values for your sort key.如果您想要一个可预测的排序顺序,您必须在查询中包含一个明确的sort()并为您的排序键提供唯一值。

How do capped collections maintain insertion order?封顶的 collections 如何维护插入顺序?

The implementation exception noted for natural order in capped collections is enforced by their special usage restrictions: documents are stored in insertion order but existing document size cannot be increased and documents cannot be explicitly deleted.上限 collections 中的自然顺序的实现异常是由它们的特殊使用限制强制执行的:文档按插入顺序存储,但现有文档大小不能增加,并且不能显式删除文档。 Ordering is part of the capped collection design that ensures the oldest documents "age out" first.排序是封顶集合设计的一部分,可确保最旧的文档首先“过期”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM