Optimize away { $expr : true } in $match clauses

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Fixed
    • Priority: Major - P3
    • 6.1.0-rc0
    • Affects Version/s: 5.0.6
    • Component/s: None
    • Fully Compatible
    • 161
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

      { $expr : true }

      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": [ ]
        },
      ...
      

            Assignee:
            Henri Nikku
            Reporter:
            James Kovacs
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: