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

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

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • 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@mongodb.com David Storch
            Reporter:
            angly Angly Cat
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: