Skip to main content

$sum

1 min read

$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 TypeSupported
TEXTNo
U64/I64/F64Yes
DATEYes
BOOLYes
KEYWORDNo
FACETNo

Field must be FAST.

Arguments#

ArgumentTypeRequiredDescription
fieldstringYesField to aggregate.
missingnumberNoFallback value for missing fields.
nullIfNoMatchbooleanNoReturn null instead of 0 when no values are collected. Default: false.

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.

Was this page helpful?