ISSUE DESCRIPTION AND IMPACT
Certain queries involving a collated index and sorting on the collated fields can return incorrectly sorted results. This impacts $or and $in operations where each clause is indexed in a way that does not require a blocking sort. The set of results is accurate, but the documents are not ordered correctly.
The bug is that the SORT_MERGE query plan stage incorrectly interprets the collated index keys.
DIAGNOSIS AND AFFECTED VERSIONS
All versions are affected. You can confirm a collation query is affected by using explain(). If a SORT_MERGE stage is in the query plan, and at least one child of the SORT_MERGE stage is an IXSCAN without a FETCH, then the query can return incorrectly sorted results.
REMEDIATION AND WORKAROUNDS
This issue will be corrected in the 4.4.2, 4.2.11, 4.0.21, and 3.6.21 versions of MongoDB. Until these versions are released, the main workaround is to sort results on the client side until a fix version is released.
original description
Given a compound index with a non-simple collation, a multi-point query on a prefix fields together with a sort on a prefix of suffix fields produces incorrect sort results when a collation is specified that matches the collation of the index and a sort merge (SORT_MERGE) is selected in the query execution plan. For example, if a collection has a compound index with a non-simple collation on fields a, b, c, d, then a find command with a matching collation specified that has a multi-point query on a and b fields and is sorted on field c will not produce correctly sorted result.