- Reference >
- Operators >
- Aggregation Pipeline 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.
$reduce
has the following syntax:Field Type Description input
array 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
null
or refers to a missing field,$reduce
returnsnull
.If the argument does not resolve to an array or
null
nor refers to a missing field,$reduce
returns an error.initialValue
expression The initial cumulative value
set beforein
is applied to the first element of theinput
array.in
expression A valid expression that
$reduce
applies to each element in theinput
array in left-to-right order. Wrap theinput
value with$reverseArray
to yield the equivalent of applying the combining expression from right-to-left.During evaluation of the
in
expression, two variables will be available:If
input
resolves to an empty array,$reduce
returnsinitialValue
.
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
$group
to group by theexperimentId
and use$push
to create an array with the probability of each event. - Use
$reduce
with$multiply
to multiply and combine the elements ofprobabilityArr
into 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: