Too many documents examined when using an index and $first/$last in $group stage

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Duplicate
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Let's say I have two documents in a collection:
      {_id: 0, a: 1, b: 1} and {_id: 1, a: 1, b: 2}
      and index on {a: 1}.
      Aggregation pipeline [{$sort: {a: 1}}, {$group: {_id: '$a'}] will be covered by the index, however pipeline [{$sort: {a: 1}}, {$group: {_id: '$a', b: {$first: '$b'}}}], according to execution stats, will examine both 2 documents despite only needing 1 value for $first operator.
      So this seems like a place for aggregation optimization.

              Assignee:
              David Storch
              Reporter:
              Angly Cat
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: