In MongoDB, sort operations can obtain the sort order by retrieving
documents based on the ordering in an index. If the query planner
cannot obtain the sort order from an index, it will sort the results in
memory. Sort operations that use an index often have better performance
than those that do not use an index. In addition, sort operations that
do not use an index will abort when they use 32 megabytes of memory.

You can specify a sort on all the keys of the index or on a subset;
however, the sort keys must be listed in the same order as they
appear in the index. For example, an index key pattern {a:1,b:1} can support a sort on {a:1,b:1} but not on {b:1,a:1}.

For a query to use a compound index for a sort, the specified sort direction
for all keys in the cursor.sort() document must match the index
key pattern or match the inverse of the index key pattern.
For example, an index key pattern {a:1,b:-1} can
support a sort on {a:1,b:-1} and {a:-1,b:1} but not
on {a:-1,b:-1} or {a:1,b:1}.

If the sort keys correspond to the index keys or an index prefix,
MongoDB can use the index to sort the query results. A prefix of a
compound index is a subset that consists of one or more keys at the
start of the index key pattern.

In such cases, MongoDB can use the index to retrieve the documents in
order specified by the sort. As the example shows, the index prefix in
the query predicate can be different from the prefix in the sort.

As the last operation shows, only the index fields preceding the sort
subset must have the equality conditions in the query document; the
other index fields may specify other conditions.

If the query does not specify an equality condition on an index
prefix that precedes or overlaps with the sort specification, the
operation will not efficiently use the index. For example, the
following operations specify a sort document of {c:1}, but the
query documents do not contain equality matches on the preceding index
fields a and b:

To use an index for string comparisons, an operation must also
specify the same collation. That is, an index with a collation
cannot support an operation that performs string comparisons on the
indexed fields if the operation specifies a different collation.

For example, the collection myColl has an index on a string
field category with the collation locale "fr".

For a compound index where the index prefix keys are not strings,
arrays, and embedded documents, an operation that specifies a
different collation can still use the index to support comparisons
on the index prefix keys.

For example, the collection myColl has a compound index on the
numeric fields score and price and the string field
category; the index is created with the collation locale
"fr" for string comparisons: