- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Array Aggregation Operators >
- $reduce (aggregation)
$reduce (aggregation)¶
On this page
Definition¶
-
$reduce¶ New in version 3.4.
Applies an expression to each element in an array and combines them into a single value.
$reducehas the following syntax:Field Type Description inputarray Can be any valid expression that resolves to an array. For more information on expressions, see Expressions.
If the argument resolves to a value of
nullor refers to a missing field,$reducereturnsnull.If the argument does not resolve to an array or
nullnor refers to a missing field,$reducereturns an error.initialValueexpression The initial cumulative valueset beforeinis applied to the first element of theinputarray.inexpression A valid expression that
$reduceapplies to each element in theinputarray in left-to-right order. Wrap theinputvalue with$reverseArrayto yield the equivalent of applying the combining expression from right-to-left.During evaluation of the
inexpression, two variables will be available:If
inputresolves to an empty array,$reducereturnsinitialValue.
| Example | Results |
|---|---|
"abc" |
|
{ "sum" : 15, "product" : 48 } |
|
[ 1, 2, 3, 4, 5, 6 ] |
Examples¶
Multiplication¶
Probability¶
A collection named events contains the events of a probability
experiment. Each experiment can have multiple events, such as
rolling a die several times or drawing several cards (without replacement)
in succession to achieve a desired result. In order to obtain the
overall probability of the experiment, we will need to multiply the
probability of each event in the experiment.
Steps:
- Use
$groupto group by theexperimentIdand use$pushto create an array with the probability of each event. - Use
$reducewith$multiplyto multiply and combine the elements ofprobabilityArrinto a single value and project it.
The operation returns the following:
Discounted Merchandise¶
A collection named clothes contains the following documents:
Each document contains a discounts array containing the currently
available percent-off coupons for each item. If each discount can be
applied to the product once, we can calculate the lowest price by using
$reduce to apply the following formula for each element in the
discounts array: (1 - discount) * price.
The operation returns the following:
String Concatenation¶
A collection named people contains the following documents:
The following example reduces the hobbies array of strings into a single string
bio:
The operation returns the following:
Array Concatenation¶
A collection named matrices contains the following documents:
Computing a Single Reduction¶
The following example collapses the two dimensional arrays into a single array collapsed:
The operation returns the following:
Computing a Multiple Reductions¶
The following example performs the same two dimensional array collapse as the example above, but also creates a new array containing only the first element of each array.
The operation returns the following: