[SERVER-36927] Projection operator to return all and only array elements which matched filter in queries. Created: 29/Aug/18  Updated: 04/Nov/18  Resolved: 01/Oct/18

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

Type: New Feature Priority: Major - P3
Reporter: Joe Langeway at Live Earth Assignee: Nick Brewer
Resolution: Won't Fix Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

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?

 



 Comments   
Comment by Nick Brewer [ 01/Oct/18 ]

jlangewayle Since there's been no activity on this ticket in some time, I'm going to close it. Let us know if you have any questions.

-Nick

Comment by Nick Brewer [ 30/Aug/18 ]

jlangewayle As you've noted, aggregation is the way to accomplish this - there's currently not any plans to implement similar functionality in find.

What use case you're hitting where aggregation isn't possible?

Thanks,
-Nick

Comment by Joe Langeway at Live Earth [ 29/Aug/18 ]

I apologize, the wysiwyg editor mucked up the bits that were supposed to be preformatted. I expect most readers will still get the point.

Generated at Thu Feb 08 04:44:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.