diff options
-rw-r--r-- | jstests/core/collation.js | 14 | ||||
-rw-r--r-- | src/mongo/db/query/query_planner.cpp | 6 |
2 files changed, 18 insertions, 2 deletions
diff --git a/jstests/core/collation.js b/jstests/core/collation.js index af87212346c..6b13cceadfd 100644 --- a/jstests/core/collation.js +++ b/jstests/core/collation.js @@ -1975,6 +1975,20 @@ .itcount()); assert.commandFailedWithCode(err, 51174); + // This query should fail, because the hinted index does not match the requested + // collation, and the 'max' value is a string, which means we cannot ignore the + // collation. + const caseInsensitive = {locale: "en", strength: 2}; + assert.commandWorked(coll.dropIndexes()); + assert.commandWorked(coll.createIndex({str: 1})); + err = assert.throws(() => coll.find({}, {_id: 0}) + .min({str: MinKey}) + .max({str: "Hello1"}) + .hint({str: 1}) + .collation(caseInsensitive) + .toArray()); + assert.commandFailedWithCode(err, 51174); + // After building an index with the case-insensitive US English collation, the query should // work. Furthermore, the bounds defined by the min and max should respect the // case-insensitive collation. diff --git a/src/mongo/db/query/query_planner.cpp b/src/mongo/db/query/query_planner.cpp index 655a6816194..9e8349b18d5 100644 --- a/src/mongo/db/query/query_planner.cpp +++ b/src/mongo/db/query/query_planner.cpp @@ -657,8 +657,10 @@ StatusWith<std::vector<std::unique_ptr<QuerySolution>>> QueryPlanner::plan( BSONObj minObj = query.getQueryRequest().getMin(); BSONObj maxObj = query.getQueryRequest().getMax(); - if (!indexCompatibleMaxMin( - minObj.isEmpty() ? maxObj : minObj, query.getCollator(), *hintedIndexEntry)) { + if ((!minObj.isEmpty() && + !indexCompatibleMaxMin(minObj, query.getCollator(), *hintedIndexEntry)) || + (!maxObj.isEmpty() && + !indexCompatibleMaxMin(maxObj, query.getCollator(), *hintedIndexEntry))) { return Status(ErrorCodes::Error(51174), "The index chosen is not compatible with min/max"); } |