- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $avg (aggregation)
$avg (aggregation)¶
On this page
Definition¶
Changed in version 5.0.
-
$avg¶
Returns the average value of the numeric values. $avg
ignores non-numeric values.
$avg is available in these stages:
$addFields(Available starting in MongoDB 3.4)$bucket$bucketAuto$group$matchstage that includes an$exprexpression$project$replaceRoot(Available starting in MongoDB 3.4)$replaceWith(Available starting in MongoDB 4.2)$set(Available starting in MongoDB 4.2)$setWindowFields(Available starting in MongoDB 5.0)
In MongoDB 3.2 and earlier, $avg is available in the
$group stage only.
Syntax¶
When used in the $bucket, $bucketAuto,
$group, and $setWindowFields stages,
$avg has this syntax:
When used in other supported stages, $avg has one of two
syntaxes:
$avghas one specified expression as its operand:$avghas a list of specified expressions as its operand:
For more information on expressions, see Expressions.
Behavior¶
Non-numeric or Missing Values¶
$avg ignores non-numeric values, including missing values. If all of the
operands for the average are non-numeric, $avg returns
null since the average of zero values is undefined.
Array Operand¶
In the $group stage, if the expression resolves to an
array, $avg treats the operand as a non-numerical value.
In the other supported stages:
- With a single expression as its operand, if the expression resolves
to an array,
$avgtraverses into the array to operate on the numerical elements of the array to return a single value. - With a list of expressions as its operand, if any of the expressions
resolves to an array,
$avgdoes not traverse into the array but instead treats the array as a non-numerical value.
Examples¶
Use in $group Stage¶
Consider a sales collection with the following documents:
Grouping the documents by the item field, the following operation
uses the $avg accumulator to compute the average amount and
average quantity for each grouping.
The operation returns the following results:
Use in $project Stage¶
A collection students contains the following documents:
The following example uses the $avg in the
$project stage to calculate the average quiz scores, the
average lab scores, and the average of the final and the midterm:
The operation results in the following documents:
Use in $setWindowFields Stage¶
New in version 5.0.
Create a cakeSales collection that contains cake sales in the states
of California (CA) and Washington (WA):
This example uses $avg in the $setWindowFields
stage to output the moving average for the cake sales quantity for
each state:
In the example:
partitionBy: "$state"partitions the documents in the collection bystate. There are partitions forCAandWA.sortBy: { orderDate: 1 }sorts the documents in each partition byorderDatein ascending order (1), so the earliestorderDateis first.outputsets theaverageQuantityForStatefield to the moving averagequantityusing$avgfor the documents in a documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument in the output. This means$avgreturns the moving averagequantityfor the documents between the beginning of the partition and the current document.
In this output, the moving average quantity for CA and WA
is shown in the averageQuantityForState field: