reduce()
Description
This method applies the reducer Function to each Set value, starting with the first value in the set.
Each reducer invocation is passed the current accumulator acc and the first Set value, and returns a new accumulator. The new accumulator is passed to the next reducer invocation with the next Set value.
The accumulator returned by the final reducer invocation is the return
value. If the set has only one item the reducer returns the first
item. If the set is empty an error is returned. A mental model for
reduce()
is that it is fold()
where the first item is used
as the seed.
Because this method scans the full set, it returns an error if there are more than 16000 documents in the set. This method can timeout for large sets under that limit.
Examples
-
For these examples, the product prices are:
{ data: [ { price: 6.98 }, { price: 24.99 }, { price: 4.99 }, { price: 3.99 }, { price: 0.35 }, { price: 3.49 }, { price: 2.99 }, { price: 1.49 }, { price: 23.99 } ] }
-
Count the prices in the set:
9
-
Find the maximum price in the set:
24.99
-
Calculate the average price:
{ count: 9, total: 73.26, average: 8.14 }
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!