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

aggregation framework should have a $find expression

    XMLWordPrintable

    Details

      Description

      Aggregation framework has a filter, which filters an array (and returns an array).

      I wish there was an equivalent to do a javascript Array.find(), where the filter would return the first match inside of the array

      db.test.insert({people: [{name: 'bob', value: 1}, {name: 'fred', value: 2}]});
       
      db.test.aggregate([{$addFields: {
        person: {
          $find: {
            input: '$people',
            cond: {$eq: ['$$this.name', 'fred']}
          }
        },
        people: '$$REMOVE'
      }}]);
       
      ##returns
      {person: {name: 'fred', value: 2}}

       

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                1 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated: