Back to Sails

`.avg()`

docs/reference/waterline/models/avg.md

12.12.20002.5 KB
Original Source

.avg()

Get the aggregate mean of the specified attribute across all matching records.

usage
var average = await Something.avg(numericAttrName, criteria);

Usage

ArgumentTypeDetails
1numericAttrName((string))The name of the numeric attribute whose mean will be calculated.
2criteria((dictionary?))The Waterline criteria to use for matching records in the database. If no criteria is specified, the average will be computed across all of this model's records. avg queries do not support pagination using skip and limit or projections using select.
Result
TypeDescription
((number))The aggregate mean of the specified attribute across all matching records.
Errors
NameTypeWhen?
UsageError((Error))Thrown if something invalid was passed in.
AdapterError((Error))Thrown if something went wrong in the database adapter.
Error((Error))Thrown if anything else unexpected happens.

See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.

Example

Get the average balance of bank accounts owned by people between the ages of 35 and 45.

javascript
var averageBalance = await BankAccount.avg('balance')
.where({
  ownerAge: { '>=': 35, '<=': 45 }
});

Notes

  • 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 return 0 for consistency and type safety in app-level code.
<docmeta name="displayName" value=".avg()"> <docmeta name="pageType" value="method">