For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

$avg (accumulator operator)

$avg

Returns the average of numeric values across a group of documents.

The $avg accumulator is available in these stages:

Note

Disambiguation

This page describes $avg when used as an accumulator. Accumulators return a single aggregated value like sum, maximum, or minimum across a group of input documents.

You can also use $avg in these other contexts:

{ $avg: <expression> }

The default return type is a double. If at least one input value is a decimal, then the return type is a decimal.

$avg ignores non-numeric values, including missing values. If all input values are non-numeric, $avg returns null.

If the operand resolves to an array, $avg treats the operand as a non-numerical value.

Create a sales collection with the following documents:

db.sales.insertMany( [
{ _id: 1, item: "abc", price: 10, quantity: 2, date: ISODate("2014-01-01T08:00:00Z") },
{ _id: 2, item: "jkl", price: 20, quantity: 1, date: ISODate("2014-02-03T09:00:00Z") },
{ _id: 3, item: "xyz", price: 5, quantity: 5, date: ISODate("2014-02-03T09:05:00Z") },
{ _id: 4, item: "abc", price: 10, quantity: 10, date: ISODate("2014-02-15T08:00:00Z") },
{ _id: 5, item: "xyz", price: 5, quantity: 10, date: ISODate("2014-02-15T09:12:00Z") }
] )

The following operation:

  1. Groups the documents by the item field.

  2. Uses the $avg accumulator to compute the average amount and average quantity for each group.

db.sales.aggregate(
[
{
$group:
{
_id: "$item",
avgAmount: { $avg: { $multiply: [ "$price", "$quantity" ] } },
avgQuantity: { $avg: "$quantity" }
}
}
]
)

Output:

[
{ _id: 'xyz', avgAmount: 37.5, avgQuantity: 7.5 },
{ _id: 'abc', avgAmount: 60, avgQuantity: 6 },
{ _id: 'jkl', avgAmount: 20, avgQuantity: 1 }
]