Definition
Returns the sum of numeric values from documents in a particular window.
Note
Other Uses of $sum
This page describes $sum when used as a window function.
You can also use $sum in these other contexts:
$sum (accumulator), which returns the collective sum of numeric values across a group of documents.$sum (expression), which returns the sum of numeric values in an array.
Syntax
{ $setWindowFields: { ... output: { <field>: { $sum: <expression> } } } }
Behavior
Result Type
When input types are mixed, $sum promotes the smaller input type to the larger of the two. A type is considered larger when it represents a wider range of values. The order of numeric types from smallest to largest is: integer → long → double → decimal.
The larger of the input types also determines the result type unless the operation overflows and is beyond the range represented by that larger data type. In cases of overflow, $sum promotes the result according to the following order:
Null or Missing Values
If some documents in the window have a null value for the field or are missing the field, $sum ignores those values and sums the remaining values.
If all documents in the window have a null value for the field or are missing the field, $sum returns 0.
Example
The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.
This example uses $setWindowFields to calculate a running cumulative vote total for each Musical movie from 2009 and 2010. Each document receives the $sum value computed from the beginning of its yearly partition through the current document.
db.movies.aggregate( [ { $match: { genres: "Musical", year: { $in: [ 2009, 2010 ] }, "imdb.rating": { $type: "double" }, "imdb.votes": { $gt: 0 } } }, { $setWindowFields: { partitionBy: "$year", sortBy: { title: 1 }, output: { cumulativeVotesInYear: { $sum: "$imdb.votes", window: { documents: [ "unbounded", "current" ] } }, rankInYear: { $documentNumber: {} } } } }, { $project: { _id: 0, title: 1, year: 1, "imdb.votes": 1, cumulativeVotesInYear: 1, rankInYear: 1 } }, { $sort: { year: 1, title: 1 } } ] )
The preceding pipeline:
Uses
$matchto filter for Musical movies from 2009 and 2010 with a numeric IMDb rating and at least one vote.Uses
$setWindowFieldsto partition movies byyearand sort them bytitle. The stage uses$sumwith a documents window of[ "unbounded", "current" ]to create the following fields for each document:cumulativeVotesInYear: the running cumulative sum ofimdb.votes.rankInYear: a per-partition row number assigned by$documentNumber.
Uses
$projectto includetitle,year,imdb.votes,cumulativeVotesInYear, andrankInYearin the output.Uses
$sortto sort results by year and title.