Skip to main content

$sum

The $sum operator returns the sum of all numeric input values.

Input

  • An array of numeric values to sum

Output

  • The sum of all input values (returns 0 if no values provided)

Examples

Sum of Multiple Values

{
  "$sum": [
    1,
    2,
    3,
    4
  ]
}
Result: 10

Sum of Field Values

{
  "$sum": [
    {
      "$input": "value1"
    },
    {
      "$input": "value2"
    },
    {
      "$input": "value3"
    }
  ]
}
Result: The sum of value1, value2, and value3 fields

No Values

{
  "$sum": []
}
Result: 0