Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-30474

leading $facet with each facet beginning with $match should add $match before $facet

    • Query Optimization

      Imagine you have an aggregation pipeline that looks something like:

      db.collection.aggregate([
        {$facet: {
          xs: [
            {$match: {x: "SOMETHING"}},
            {$group: {...}}
          ],
          ys: [
            {$match: {y: "SOMETHING ELSE"}},
            {$unwind: "$y"},
            ...
          ]
        }}
      ])
      

      Such a pipeline would not provide any predicates for the query system to use to begin the pipeline. This pipeline might benefit from being optimized to something more like:

      db.collection.aggregate([
        {$match: {$or: [{x: "SOMETHING"}, {y: "SOMETHING ELSE"}]}},
        {$facet: {
          xs: [
            {$match: {x: "SOMETHING"}},
            {$group: {...}}
          ],
          ys: [
            {$match: {y: "SOMETHING ELSE"}},
            {$unwind: "$y"},
            ...
          ]
        }}
      ])
      
      Original Description

      $facet is not smart enough to use an index. if the only stage in a pipeline is $facet, the planning system will simply default to a COLLSCAN.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            vick.mena Vick Mena (Inactive)
            Votes:
            9 Vote for this issue
            Watchers:
            28 Start watching this issue

              Created:
              Updated: