- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $sort (aggregation)
$sort (aggregation)¶
Definition¶
-
$sort¶ Sorts all input documents and returns them to the pipeline in sorted order.
The
$sortstage has the following prototype form:$sorttakes a document that specifies the field(s) to sort by and the respective sort order.<sort order>can have one of the following values:Value Description 1Sort ascending. -1Sort descending. { $meta: "textScore" }Sort by the computed textScoremetadata in descending order. See Metadata Sort for an example.If sorting on multiple fields, sort order is evaluated from left to right. For example, in the form above, documents are first sorted by
<field1>. Then documents with the same<field1>values are further sorted by<field2>.
Examples¶
Ascending/Descending Sort¶
For the field or fields to sort by, set the sort order to 1 or -1 to
specify an ascending or descending sort respectively, as in the following example:
This operation sorts the documents in the users collection,
in descending order according by the age field and then in
ascending order according to the value in the posts field.
When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest:
- MinKey (internal type)
- Null
- Numbers (ints, longs, doubles, decimals)
- Symbol, String
- Object
- Array
- BinData
- ObjectId
- Boolean
- Date
- Timestamp
- Regular Expression
- MaxKey (internal type)
For details on the comparison/sort order for specific types, see Comparison/Sort Order.
Metadata Sort¶
Specify in the { <sort-key> } document, a new field name for the
computed metadata and specify the $meta expression as its
value, as in the following example:
This operation uses the $text operator to match the documents,
and then sorts first by the "textScore" metadata and then by
descending order of the posts field. The specified metadata
determines the sort order. For example, the "textScore" metadata
sorts in descending order. See $meta for more information
on metadata.
$sort Operator and Memory¶
$sort + $limit Memory Optimization¶
When a $sort precedes a $limit and there are no
intervening stages that modify the number of documents, the optimizer can
coalesce the $limit into the $sort. This allows
the $sort operation to only
maintain the top n results as it progresses, where n is the
specified limit, and ensures that MongoDB only needs to store n items in memory.
This optimization still applies when allowDiskUse is true and
the n items exceed the aggregation memory limit.
Optimizations are subject to change between releases.
$sort and Memory Restrictions¶
The $sort stage has a limit of 100 megabytes of RAM for
in-memory sorts. By default, if the stage exceeds this limit,
$sort produces an error. To allow pipeline processing to
take up more space, use the allowDiskUse option to enable aggregation pipeline
stages to write data to temporary files.
See also
$sort Operator and Performance¶
The $sort operator can take advantage of an index if it’s
used in the first stage of a pipeline or if it’s only preceeded by a
$match stage.
When you use the $sort on a sharded cluster, each shard
sorts its result documents using an index where available. Then the
mongos or one of the shards performs a streamed merge
sort.