.sum()
Get the aggregate sum of the specified attribute across all matching records.
var total = await Something.sum(numericAttrName, criteria);
Argument | Type | Details | |
---|---|---|---|
1 | numericAttrName | The name of the numeric attribute to be summed. | |
2 | criteria | The Waterline criteria to use for matching records in the database. If no criteria is specified, the sum will be computed across all of this model's records. sum queries do not support pagination using skip and limit or projections using select . |
Type | Description |
---|---|
The aggregate sum of the specified attribute across all matching records. |
Name | Type | When? |
---|---|---|
UsageError | Thrown if something invalid was passed in. | |
AdapterError | Thrown if something went wrong in the database adapter. | |
Error | Thrown if anything else unexpected happens. |
See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.
Get the cumulative account balance of all bank accounts that have less than $32,000 or are flagged as "suspended".
var total = await BankAccount.sum('balance')
.where({
or: [
{ balance: { '<': 32000 } },
{ suspended: true }
]
});
- This method can be used with
await
, promise chaining, or traditional Node callbacks.- Some databases, like MySQL, may return
null
for this kind of query; however, it's best practice for Sails/Waterline adapter authors to return0
for consistency and type safety in app-level code.