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

Make $[] operator from update operations available in find projections

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Execution

      The $elemMatch operator allows users to query for documents where at least 1 element matches all criteria in the query.

      The positional $ projection operator, when used with $elemMatch allows projecting a result array such that it only contains the first element that matches, filtering out any non-matches and subsequent matches.

      In situations where multiple documents may match and all are desired, the best alternatives I can find are:

      • re-implementing the query on the application to filter the results array again
      • rewriting a query into an aggregation, first with a match stage, followed by an $unwind, and followed by the same match again

      Whereas in update, updating all documents that match an elemMatch filter is possible with $[]

      Being able to specify a regular find with a projection of $[] to return all matching elements would make this a significantly simpler operation

      https://www.mongodb.com/docs/drivers/node/current/fundamentals/crud/write-operations/embedded-arrays/#matching-all-array-elements
      https://www.mongodb.com/docs/manual/reference/operator/projection/positional/
      https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            jack.wearden@mongodb.com Jack Wearden
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: