Optimize $filter + $arrayElemAt to avoid scanning entire array

XMLWordPrintableJSON

    • Query Optimization
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      If you have a very large array, and want to extract the first element matching some predicate, you should be able to do so without scanning the entire array.

      We should optimize the combination of $arrayElemAt and $filter to do so.

      Original Description

      Why can't $elemMatch be used in the $project phase? It would allow me to easily select an element out of an array.

      Example:

      db.person.aggregate([
        { "$project": {'main_event' :
          {'$events_array': {'$elemMatch' : {
            'event_status' : 'main'
          }}}
        }}
      ]);
      

      Basically what I'm looking for is an easier way to convert an array into a scalar without having to $unwind and then re-$group, which is taxing for proc time.

              Assignee:
              [DO NOT USE] Backlog - Query Optimization
              Reporter:
              Xavier Del Castillo
              Votes:
              1 Vote for this issue
              Watchers:
              9 Start watching this issue

                Created:
                Updated: