Let's take a moment to learn about using Accumulator Expressions with the $project stage.

Knowledge of how to use these expressions can greatly simplify our work.

One important thing to keep in mind is that Accumulator Expressions

within $project work over an array within the given document.

They do not carry values forward to each document encountered.

Let's suppose we have a collection named example with the schema.

If we perform this aggregation,

this will be the result,

an output document for

every input document with the average of that document's data field.

For this lesson, we're going to explore this data set.

As the average monthly low and high temperature for the United States,

as well as monthly ice cream consumer price index and sales information.

And here's what the data looks like in our collection.

We can see we have the trends array with

documents that contain all the information we'll need,

easy enough to work with.

Let's go ahead and find the maximum and minimum values for the average high temperature.

We'll explore two different methods to find the maximum.

First, we'll use the $reduce expression to manually find the maximum.

Before I run this, let's break it down.

Here, I'm specifying the $reduce expression.

$reduce takes an array as a simpler argument here.

For the argument to initial value,

the value or accumulator we'll begin with,

we're specifying negative Infinity.

I hope we'll never have monthly average high temperature of negative Infinity,

but in all seriousness,

we're using negative Infinity because

any reasonable value we encounter should be greater.

Lastly, we'll specify the logic to the in field here.

This is using the $cond conditional operator and saying,

if $$this.avg_high_tmp is greater than

the $$value which is held in our accumulator, then return $$this.avg_high_tmp.

Otherwise, just return the $$value back.

So, compare the current value against the accumulator value and if it's greater,

we'll replace it with a value which we just encountered.

Otherwise, we'll just keep using our current max value.

Notice the double dollar signs.

These are temporary variables defined for use only within the

$reduce expression as we mentioned in the arrogation structure and syntax lesson.

$$this refers to the current element in the array.

$$value refers to the accumulator value.

It will do this for every element in the array.

Okay, let's run this.

And we see the max_high was 87.

Wow, that was pretty complicated.

Let's look at an easier way to accomplish this.

I think we can all agree that this is much simpler.

We use the $max group accumulator expression to get the information we want.

And again, we get max_high of 87.

Okay, let's get the minimum average temperature.

Here, we use the $min accumulator expression,

and we can see our max_low was 27.

All right, we now know how to use max and min.

We can also calculate averages and standard deviations.

Let's calculate the average consumer price index for

ice cream as well as the standard deviation.

Here, we're calculating both in one pass.

For the average_cpi field,

we specified the $avg,

average expression, telling it to average of

the values in the icecream_cpi field in the $trends array.

And here, the cpi_deviation is calculated almost identically,

except we're using the population standard deviation.

We're using $stdDevPop because we're looking at the entire set of data.

However, if this was only a sample of our data,

we'd use a sample standard deviation expression.

Great. We can see that the average consumer price index was

221.275 and the standard deviation was around 6.63.

We could use this information to find data that is

outside norms to point to areas that might need special analysis.

The last accumulator expression I'd like to show is $sum.

As the name implies,

$sum sums up the values of an array.

We can see that the yearly_sales were 1,601 million,

and that covers Accumulator Expressions available within $project.

Here are a few things to keep in mind.

The Available Accumulator Expressions in $project are $sum, $avg,

$max, $min, $stdDevPop, and $stdDevSam.

With in $project., these expressions will

not carry their value forward and operate across multiple documents.

For this, we need to use the unwind stage and group accumulator expressions.

For more complex calculations,

it's handy to know how to use $reduce and $map.