[SERVER-2142] Allow the ability to return a subset of the elements in an array property Created: 24/Nov/10 Updated: 16/Nov/21 Resolved: 24/Sep/13 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Ryan Wynn | Assignee: | Unassigned |
| Resolution: | Duplicate | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Participants: |
| Description |
|
Given a websites collection with a document { _id: 0, domain: "mongodb.org", "like": [1, 2, 3, 4, 5] }where like represents the ids of all users who have "liked" the document, Allow a query such as: db.websites.find( { domain : "mongodb.org'' }, { like : {$in : [4, 5, 6] } } ); resulting in { _id:0, domain: "mongodb.org", "like": [4, 5] }Currently, the only way to do this is to filter client side or store the data in a more normalized fashion. |
| Comments |
| Comment by Ian Whalen (Inactive) [ 24/Sep/13 ] |
|
Done with elemMatch (http://docs.mongodb.org/manual/reference/operator/elemMatch/) |
| Comment by Juraj Vitko [ 06/Nov/11 ] |
|
This would be especially useful when instead of the numbers [1, 2, 3, 4, ...] there are actual objects. Returning the whole array in such case is wasteful, if the array is big. Similarly, it would be great if the .$. operator was usable for filtering the returned fields, e.g. db.col = [ { subarr: [ { id: 'obj1' }, { id: 'obj2' }] }, ... ]; db.col.find( { 'subarr.id': 'obj2' }, { 'subarr.$.id': 1 })...* ..meaning: only return the matched element of the matched obj.array, and only its 'id' field (in this example). Since it's not possible to use the field filtering this way, the whole array is returned and the client needs to do the searching in it again. I'm not sure if I should open a new ticket for this, I think it's the same issue. *actually a syntax like 'subarr[$].id' might be more intuitive there (in the returned field subset filtering clause). |
| Comment by Eliot Horowitz (Inactive) [ 24/Nov/10 ] |
|
I think there are other cases that cover this - but can't find them at the moment |