Skip to main content
$sum computes the total of a field across matching documents. If missing is set, missing fields contribute that fallback value. By default, $sum returns 0 when no values are collected — either no documents match, or the matching documents all have a missing/null value for the field and no missing fallback is set. Set nullIfNoMatch to true to return null instead, so you can distinguish that case from a real total of zero.

Compatibility

Field must be FAST.

Arguments

Output

With nullIfNoMatch: true, an aggregation with no collected values (for example, the filter above matches no documents) returns:
Without nullIfNoMatch, the same aggregation returns 0. This option does not change results where at least one value was collected, including a real sum of zero.