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/