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

Add $any/$first/$oneOf operator to projection

    XMLWordPrintable

    Details

      Description

      It would be great if instead of making a long list of conditionals you could use any $any operator to get the first of the fields that exists.

      Ex:

      var docs = [
        { 'a': true },
        { 'b': true },
        { 'a': true}
        ...
      ]

      { '$project':
        { 'wow':
          { '$any': [ '$a', '$b', '$b.nested', ... ] }
        }
      }

      This would make it easy to project an _id for use by a $group stage in situations where you have varied document schema.

      For example, the next stage of the pipeline could be:

      { $group: 
        { 
          _id: { wow: '$wow' },
          count: { '$sum': 1 }
        }
      }

        Attachments

          Activity

            People

            • Votes:
              3 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated: