Skip to content

sum - Calculate the sum value

The sum function will return the total of all values passed into the function. It is most typically used as an aggregation.

Syntax

sum(value: number, ...values: number): number

Arguments

Name Type Required Description
value number true The first numerical value to total
...values number true All remaining numerical values to total up

Example - Working out total money spent for a given user

Consider the following documents:

{
    "user": "Chris",
    "product": "Sunglasses",
    "price": 10
},
{
    "user": "Chris",
    "product": "Car",
    "price": 1000
},
{
    "user": "Dave",
    "product": "Desk",
    "price": 250
},

We can summate total money spent, by using sum on the price field, in conjunction with an aggregation on the user field. In this case, we'll use the top command to get the top 10 spenders.

top 10 user by sum(price)

This will give the top 10 users who spent the most money.