[SERVER-9334] Aggregation framework - operator for adding vectors/array Created: 11/Apr/13 Updated: 06/Dec/22 Resolved: 13/Sep/17 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | 3.4.0 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Matt Kalan | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Done | Votes: | 4 |
| Labels: | expression | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Assigned Teams: |
Query
|
||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||
| Participants: | |||||||||||||||||
| Description |
|
It would be very useful to be able to do operations on arrays as if they were vectors, in particular to add them. So this would add the 0th entry of the arrays in the pipeline and then the 1st, 2nd, etc. The result will be one document with an array with each entry being the sum of all the items in that same position in the source documents. This can be useful in doing risk calculations in financial services. This could be done today with unwinding which doesn't perform as well as this would. |
| Comments |
| Comment by Asya Kamsky [ 13/Sep/17 ] | ||
|
This is possible as of 3.4 with something like the following to sum to arrays "a" and "b":
| ||
| Comment by Asya Kamsky [ 04/Jan/17 ] | ||
|
Correct - you can use $zip options useLongestLength and defaults to take care of arrays (vectors) being of different length and to provide default value to $add in that case. You would only use $reduce if you needed to sum all of the arrays - I think you're looking for $map and $sum where $zip produces the "input" to $map. See first example here: http://www.kamsky.org/stupid-tricks-with-mongodb/using-34-aggregation-enhancements-for-parallel-array-processing | ||
| Comment by Matt Kalan [ 03/Jan/17 ] | ||
|
To confirm, you would do a $zip passed to $reduce with $add to add the 2 original vectors, correct? I'm not sure how we process it but that could be two passes of the vectors instead of merging them in one pass | ||
| Comment by Asya Kamsky [ 03/Jan/17 ] | ||
|
Now that 3.4 has $zip expression for parallel array processing, can this ticket be closed? |