Back to Sails

`.count()`

docs/reference/waterline/models/count.md

12.12.20001.9 KB
Original Source

.count()

Get the total number of records matching the specified criteria.

usage
var numRecords = await Model.count(criteria);

Usage

#ArgumentTypeDetails
1criteria((dictionary?))The Waterline criteria to use for matching records in the database. Note that count queries do not support pagination using skip and limit or projections using select.
Result
TypeDescription
((number))The number of records from your database that match the given criteria.
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

javascript
var total = await User.count({name:'Flynn'});
sails.log(`There ${total===1?'is':'are'} ${total} user${total===1?'':'s'} named "Flynn".`);

Notes

<docmeta name="displayName" value=".count()"> <docmeta name="pageType" value="method">