Find records in your database that match the given criteria.
var records = await Something.find(criteria);
Argument | Type | Details | |
---|---|---|---|
1 | criteria | ((dictionary)) | The Waterline criteria to use for matching records in the database. |
Type | Description |
---|---|
((array)) of ((dictionary)) | The array of records from your database which match the given criteria. |
Name | Type | When? |
---|---|---|
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.
To find any users named Finn in the database:
var usersNamedFinn = await User.find({name:'Finn'});
sails.log('Wow, there are %d users named Finn. Check it out:', usersNamedFinn.length, usersNamedFinn);
return res.json(usersNamedFinn);
Projection selectively omits the fields returned on found records. This can be done, for example, for faster performance, or for greater security when passing found records to the client. The select clause in a Waterline criteria takes an array of strings that correspond with attribute names. The record ID is always returned.
var usersNamedFinn = User.find({
where: {name:'Finn'},
select: ['name', 'email']
});
return res.json(usersNamedFinn);
Might yield:
[
{
id: 7392,
name: 'Finn',
email: '[email protected]'
},
{
id: 4427,
name: 'Finn',
email: '[email protected]'
}
// ...more users named Finn and their email addresses
]
- This method can be used with
await
, promise chaining, or traditional Node callbacks.