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

Proposal: $find aggregation pipeline operator

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Query

      When filtering arrays we often need to reduce to the first element matching a condition. This is currently achievable with the following:

      {$project: {
        action: {$arrayElemAt: [{$filter: {
          input: '$actions',
          cond: {$eq: ['$$this.id', '$id']},
        }}, 0]},
      }}

      A cleaner and more performant option would be to have a dedicated `find` operator:

      {$project: {
        action: {$find: {
          input: '$actions',
          cond: {$eq: ['$$this.id', '$id']},
        }},
      }}

      This idea is analagous to the ECMAScript Array.prototype.find, and would compliment the '$filter', '$map', and '$reduce' operators that are already implemented.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            pete@duel.me Pete Boere
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: