[SERVER-4588] aggregation: add option to $unwind to emit array index Created: 30/Dec/11 Updated: 16/Nov/21 Resolved: 11/Sep/15 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | 3.1.8 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Daniel Pasette (Inactive) | Assignee: | Charlie Swanson |
| Resolution: | Done | Votes: | 19 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||||||||||||||||||
| Sprint: | Quint 9 09/18/15 | ||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||
| Description |
|
There may be times when the index of an array element is interesting. $unwind instead of just
consider also supporting something like this:
The effect of this is the same except that it also adds a new field index |
| Comments |
| Comment by Githook User [ 13/Oct/15 ] | ||||||||||
|
Author: {u'username': u'cswanson310', u'name': u'Charlie Swanson', u'email': u'charlie.swanson@mongodb.com'}Message: Adds logic to $lookUp to handle the includeArrayIndex parameter from | ||||||||||
| Comment by Githook User [ 11/Sep/15 ] | ||||||||||
|
Author: {u'username': u'cswanson310', u'name': u'Charlie Swanson', u'email': u'charlie.swanson@mongodb.com'}Message: Adds a includeArrayIndex option to the $unwind stage. If this option is instead of just the array value. Note this does not affect the behavior non-arrays or empty arrays. If | ||||||||||
| Comment by Githook User [ 11/Sep/15 ] | ||||||||||
|
Author: {u'username': u'cswanson310', u'name': u'Charlie Swanson', u'email': u'charlie.swanson@mongodb.com'}Message: Adds logic to $lookUp to handle the includeArrayIndex parameter from | ||||||||||
| Comment by John A. De Goes [ 07/Jul/15 ] | ||||||||||
|
I'd amend my suggestion to just have a `$zip` that can zip two arrays together: {a: [1, 2, 3], b: ["a","b"]}> {ab: {$zip: ["$a", "$b"]}} > {ab: [[1, "a"], [2, "b"]]}Then if necessary a separate `$range` operator could generate an array with integral values falling between the specified upper and lower bound. These operators are more primitive and can be composed together in many more useful ways than a special case flag for $unwind. | ||||||||||
| Comment by John A. De Goes [ 24/Sep/14 ] | ||||||||||
|
More composable would be an option to bundle each index into the array elements, i.e. `zipWithIndex`: {"$zipWithIndex": "fruit_ranking"}-> {"fruit_ranking": [[0, "Apple"], [1, "Orange"], [2, "Grape"], [3, "Kiwi"], [4, "Mango"], [5, "Pear"]]}With this functionality, it becomes easy to unwind both results and extract out the index or the value or both. | ||||||||||
| Comment by Oliver Yeh [ 11/Jan/14 ] | ||||||||||
|
This would be extremely useful for me too. I have this use case. Tastiest Fruit Rankings:
I'm trying to grab the ranking of "Pear" for each day Jan 1st - Dec 31st and right now I need to grab all of the arrays back and do indexOf | ||||||||||
| Comment by Ryan Brothers [ 26/Dec/12 ] | ||||||||||
|
I agree - this would be extremely useful for me too as the array indexes returned could be used to update the matched subdocuments by using dot notation. For example, if subdocuments 0, 1, and 3 were returned from a call to aggregate with $unwind and then $match to filter the subdocuments, the matched subdocuments could be updated with:
|