[SERVER-82196] Make $[] operator from update operations available in find projections Created: 13/Oct/23  Updated: 07/Dec/23

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Jack Wearden Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 0
Labels: arrays, mql
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Assigned Teams:
Query Execution
Participants:

 Description   

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/



 Comments   
Comment by Katya Kamenieva [ 07/Dec/23 ]

I'm going to backlog this request for now. I agree we need a better UX for working with arrays, but I'm not sure we should rely on legacy update language.

Generated at Thu Feb 08 06:48:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.