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:Which of the following statements is not true about sorting data in Excel?Answer: You cannot sort data by more than one column.
Why can't I sort in Excel?Wrong Selection
If you select the wrong rows and columns or less than the full cell range that contains the information you want to sort, Microsoft Excel can't arrange your data the way you want to view it. With a partial range of cells selected, only the selection sorts. With empty cells selected, nothing happens.
How do you use the sort function in Excel?Sort text. Select a cell in the column you want to sort.. On the Data tab, in the Sort & Filter group, do one of the following: To quick sort in ascending order, click. (Sort A to Z). To quick sort in descending order, click. (Sort Z to A).. Which of the following is not a basic rule for naming a cell range?The following are not allowed: Space characters are not allowed as part of a name. Names can't look like cell addresses, such as A$35 or R2D2. C, c, R, r — can't be used as names — Excel uses them as selection shortcuts.
|