is equivalent to { }, but the two generate different query plans (see below). The .NET/C# driver will generate
{ $expr : true }if all variables are known at run-time on the client-side and evaluate to true. (Other query frameworks that generate MQL dynamically will likely do something similar.)
The .NET/C# driver is implementing a client-side optimization to render
{ $expr : true }as { } in $match clauses, but older versions (and other drivers) won't have this optimization. It is desirable for the query engine to implement this optimization itself.
Note that this is similar to SERVER-33925, which considers the case of
{ $expr : false }.
Query plans...
> db.coll.explain().aggregate([{$match:{ $expr: true }}]) { "explainVersion": "1", "queryPlanner": { "namespace": "test.coll", "indexFilterSet": false, "parsedQuery": { "$expr": { "$const": true } }, "queryHash": "0C022C46", "planCacheKey": "C3C05147", "optimizedPipeline": true, "maxIndexedOrSolutionsReached": false, "maxIndexedAndSolutionsReached": false, "maxScansToExplodeReached": false, "winningPlan": { "stage": "COLLSCAN", "filter": { "$expr": { "$const": true } }, "direction": "forward" }, "rejectedPlans": [ ] }, ...
> db.coll.explain().aggregate([{$match:{}}]) { "explainVersion": "1", "queryPlanner": { "namespace": "test.coll", "indexFilterSet": false, "parsedQuery": { }, "queryHash": "8B3D4AB8", "planCacheKey": "D542626C", "optimizedPipeline": true, "maxIndexedOrSolutionsReached": false, "maxIndexedAndSolutionsReached": false, "maxScansToExplodeReached": false, "winningPlan": { "stage": "COLLSCAN", "direction": "forward" }, "rejectedPlans": [ ] }, ...
- is related to
-
CSHARP-4116 Optimize { $expr : true } to { }
- Closed
- related to
-
SERVER-33925 Queries that are known to return no results at plan time should optimize to EOF plan
- Backlog