Details
-
Improvement
-
Resolution: Fixed
-
Major - P3
-
None
-
None
-
None
-
Query Optimization
-
Fully Compatible
-
QO 2023-12-11
Description
Currently, query on clustered collection will not use the index
db.coll.find({$expr: {$gt: ["$_id", <..>]}}) |
However, same syntax with a secondary index will work.
Because of slight semantic difference between match $gt and aggregation $gt
(match does type-bracketing, aggregation uses the specified BSON comparison order for values of different types), we have two sets of comparison match expressions:
regular ones like $gt and $_internalExprGt which use slightly different logic.
When computing bounds for secondary index scan, $_internalExpr* operators are supported, however, they are not supported in clustered collection scan bounds:
https://github.com/mongodb/mongo/blob/bc654c2bf5fb81d7767c1c25aa09628fc5de90a8/src/mongo/db/query/planner_access.cpp#L449
There might be a reason for it, because of semantic difference, so we need to either fix it by adding internal expr comparison operations to the code, linked about, or add a comment about why we can't do it.