View a markdown version of this page

$avg - Amazon DocumentDB

$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):

Node.js
const { MongoClient } = require('mongodb'); async function calculateAvgScore() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); // Accumulator usage: average across grouped documents const students = db.collection('students'); await students.insertMany([ { name: "John", score: 85 }, { name: "Jane", score: 92 }, { name: "Bob", score: 78 }, { name: "Alice", score: 90 } ]); const accumulatorResult = await students.aggregate([ { $group: { _id: null, avgScore: { $avg: '$score' } }} ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Window operator usage: running average within each partition const readings = db.collection('readings'); await 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 } ]); const windowResult = await readings.aggregate([ { $setWindowFields: { partitionBy: "$city", sortBy: { day: 1 }, output: { runningAvg: { $avg: "$temp", window: { documents: ["unbounded", "current"] } } } } } ]).toArray(); console.log('Window result:', windowResult); await client.close(); } calculateAvgScore();
Python
from pymongo import MongoClient def calculate_avg_score(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test # Accumulator usage: average across grouped documents students = db.students students.insert_many([ { 'name': 'John', 'score': 85 }, { 'name': 'Jane', 'score': 92 }, { 'name': 'Bob', 'score': 78 }, { 'name': 'Alice', 'score': 90 } ]) accumulator_result = list(students.aggregate([ { '$group': { '_id': None, 'avgScore': { '$avg': '$score' } }} ])) print('Accumulator result:', accumulator_result) # Window operator usage: running average within each partition readings = db.readings readings.insert_many([ { '_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 } ]) window_result = list(readings.aggregate([ { '$setWindowFields': { 'partitionBy': '$city', 'sortBy': { 'day': 1 }, 'output': { 'runningAvg': { '$avg': '$temp', 'window': { 'documents': ['unbounded', 'current'] } } } } } ])) print('Window result:', window_result) client.close() calculate_avg_score()