On this page
$orderbyoperator sorts the results of a query in ascending or descending order.
You can also specify the option in either of the following forms:
These examples return all documents in the collection named
collectionsorted by the
agefield in descending order. Specify a value to
$orderbyof negative one (e.g.
-1, as above) to sort in descending order or a positive value (e.g.
1) to sort in ascending order.
The sort function requires that the entire sort be able to complete within 32 megabytes. When the sort option consumes more than 32 megabytes, MongoDB will return an error.
To avoid this error, create an index to support the sort operation or use
$orderby in conjunction with
cursor.limit() increases the speed and reduces the amount of memory required to return this query by way of an optimized algorithm. The specified limit must result in a number of documents that fall within the 32 megabyte limit.