Projection operator to return all and only array elements which matched filter in queries.

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Won't Fix
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      There are a dozen issues spanning years that ask for this feature and all are incorrectly closed saying that $elemMatch or $ projection operators do this. That is wrong. Examples:
      https://jira.mongodb.org/browse/SERVER-828
      https://jira.mongodb.org/browse/SERVER-6612
      https://jira.mongodb.org/browse/SERVER-7224
      https://jira.mongodb.org/browse/SERVER-2238

      Many use cases require ALL the matching elements from an array field, not just the first.

      I have a collection with documents that look like this:

      {{{}}
         entityId: 1,
         events: [
           {eventId: 2, otherData: {,}}
           {eventId: 3, otherData: {,}}
           {eventId: 4, otherData: {,}}
           {eventId: 5, otherData: {,}}
           {eventId: 6, otherData: {}}
      ]
      {{ },{}}
         entityId: 7,
         events: [
           {eventId: 8, otherData: {,}}
           {eventId: 9, otherData: {,}}
           {eventId: 10, otherData: {,}}
           {eventId: 11, otherData: {,}}
           {eventId: 12, otherData: {}}
       ]
      {{ }}}

       

      and I'd like to do a query like:

      {{collection.find({entityId: {$in: [1, 7]}, 'events.eventId': {$in: [3, 4, 5, 9], {entityId: 1, 'events.$$': 1})}}

      which I would expect to return:

      {{{}}
         entityId: 1,
         events: [
           {eventId: 3, otherData: {,}}
           {eventId: 4, otherData: {,}}
           {eventId: 5, otherData: {}}
      {{   ]}}
      },{
         entityId: 7,
         events: [
           {eventId: 9, otherData: {}}
      {{ ]}}
      }

      This can be done with an aggregation, but that is not an acceptable work around for many use cases.

      Is there ever going to be such a feature for "find"s?

       

            Assignee:
            Nick Brewer (Inactive)
            Reporter:
            Joe Langeway at Live Earth
            Votes:
            2 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: