$avg
The $avg aggregation operator in Amazon DocumentDB calculates the average value of the specified expression across the documents that are input to the stage. This operator is useful for computing the average of a numeric field or expression across a set of documents.
Parameters
-
expression: The expression to use to calculate the average. This can be a field path (e.g."$field") or an expression (e.g.{ $multiply: ["$field1", "$field2"] }).
Example (MongoDB Shell)
The following example demonstrates how to use the $avg operator to calculate the average score across a set of student documents.
Create sample documents
db.students.insertMany([ { name: "John", score: 85 }, { name: "Jane", score: 92 }, { name: "Bob", score: 78 }, { name: "Alice", score: 90 } ]);
Query example
db.students.aggregate([ { $group: { _id: null, avgScore: { $avg: "$score" } }} ]);
Output
[
{
"_id": null,
"avgScore": 86.25
}
]
Window operator usage example (MongoDB Shell)
New from version 8.0.2.
The $avg operator can also be used as a window operator in the $setWindowFields stage. In this context, it returns the average of the specified expression for the documents in each window. You specify the operator under the output field, and optionally define the window boundaries with a window document.
Create sample documents
db.readings.insertMany([ { _id: 1, city: "Denver", day: 1, temp: 70 }, { _id: 2, city: "Denver", day: 2, temp: 80 }, { _id: 3, city: "Seattle", day: 1, temp: 60 }, { _id: 4, city: "Seattle", day: 2, temp: 64 }, { _id: 5, city: "Seattle", day: 3, temp: 68 } ]);
Query example
The following example partitions the documents by city, sorts each partition by day, and returns a running average of temp from the start of the partition through the current document.
db.readings.aggregate([ { $setWindowFields: { partitionBy: "$city", sortBy: { day: 1 }, output: { runningAvg: { $avg: "$temp", window: { documents: ["unbounded", "current"] } } } } } ]);
Output
[
{ "_id": 1, "city": "Denver", "day": 1, "temp": 70, "runningAvg": 70 },
{ "_id": 2, "city": "Denver", "day": 2, "temp": 80, "runningAvg": 75 },
{ "_id": 3, "city": "Seattle", "day": 1, "temp": 60, "runningAvg": 60 },
{ "_id": 4, "city": "Seattle", "day": 2, "temp": 64, "runningAvg": 62 },
{ "_id": 5, "city": "Seattle", "day": 3, "temp": 68, "runningAvg": 64 }
]
Each document is augmented with runningAvg, the average of temp within its partition up to and including the current document.
Code examples
To view a code example for using the $avg operator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in $group) and window operator usage (in $setWindowFields):