Docs Home → MongoDB Manual Show cursor.sort(sort)
Importantmongosh MethodThis page documents a For MongoDB API drivers, refer to the language-specific MongoDB driver documentation. Specifies the order in which the query returns matching documents. You must apply The
The The sort document can specify ascending or descending sort on existing fields or sort on text score metadata. You can sort on a maximum of 32 keys. Changed in version 4.4. MongoDB does not store documents in a collection in a particular order. When sorting on a field which contains duplicate values, documents containing those values may be returned in any order. If consistent sort order is desired, include at least one field in your sort that contains unique values. The easiest way to guarantee this is to include the Consider the following
The following command uses the
In this example, sort order may be inconsistent, since the
While the values for To achieve a consistent
sort, add a field which contains exclusively unique values to the sort. The following command uses the
Since the Specify in the sort parameter the field or fields to sort by and a value of The following operation sorts the documents first by the
When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest:
For details on the comparison/sort order for specific types, see Comparison/Sort Order. For a The following sample document specifies a descending sort by the
The For more information, see
MongoDB can obtain the results of a sort operation from an index which includes the sort fields. MongoDB may use multiple indexes to support a sort operation if the sort uses the same indexes as the query predicate. If MongoDB cannot use an index or indexes to obtain the sort order, MongoDB must perform a blocking sort operation on the data. A blocking sort indicates that MongoDB must consume and process all input documents to the sort before returning results. Blocking sorts do not block concurrent operations on the collection or database. Sort operations that use an index often have better performance than blocking sorts. For more information on creating indexes to support sort operations, see Use Indexes to Sort Query Results. If MongoDB requires using more than 100 megabytes of system memory for the blocking sort operation, MongoDB returns an error unless the query specifies To check if MongoDB must perform a blocking sort, append
To prevent blocking sorts from consuming too much memory:
TipSee also:You can use
If MongoDB cannot obtain the sort order via an index scan, then MongoDB uses a top-k sort
algorithm. This algorithm buffers the first
TipSee also:When a set of results are both sorted and projected, the MongoDB query engine will always apply the sorting first. A collection
The following query, which returns all documents from
the The query returns the documents in indeterminate order:
The following query specifies a sort on the
The query returns the following documents, in descending order of
The following query specifies the sort order using the fields from an embedded document
The query returns the following documents, ordered first by the
The Queries that include a sort by TipSee also:When sorting data on more than one field in an Access query which field is the major sort key which field is the minor sort key?When you are sorting records by more than one field (such as sorting by rep number and then by customer name), the first sort field (RepNum) is called the major sort key (also called the primary sort key) and the second sort field (CustomerName) is called the minor sort key (also called the secondary sort key).
What is a major sort key?A field or fields in a record that dictate the sequence of the file. For example, the sort keys STATE and NAME arrange the file alphabetically by name within state. STATE is the major sort key, and NAME is the minor key.
How do you sort a query in access?To sort a query in Access when in design view, click into the “Sort” row of the field in the QBE grid by which to sort the result set. Then use the drop-down that appears to select either “Ascending” or “Descending” order.
How do I sort a field in Access Report?Click and drag the field you want to sort by first to the first column of your datasheet. (You can also select the fields by clicking on the first row of any column in the datasheet, and using the drop-down menu.) From the row marked “Sort” in the datasheet's first column, select ”Ascending “ or “Descending”.
|