Skip to main content
$sum computes the total of a field across matching documents. If missing is set, missing fields contribute that fallback value.

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.
await index.aggregate({
  aggregations: {
    total_price: { $sum: { field: "price", missing: 0 } },
  },
});

Output

{ "total_price": { "value": 360 } }
value can be null when no values are available.