简体   繁体   中英

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

I am using this 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...

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. 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.

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
    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. 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. 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.

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). 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.

How do capped collections maintain insertion order?

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. Ordering is part of the capped collection design that ensures the oldest documents "age out" first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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