Edit Page

.update()

Update all records matching criteria.

await Something.update(criteria)
.set(valuesToSet);

or

Usage

#
Argument Type Details
1 criteria Dictionary The Waterline criteria to use for matching records in the database. update queries do not support pagination using skip and limit, or projections using select.
2 valuesToSet Dictionary A dictionary (plain JavaScript object) of values that all matching records should be updated to have. (Note that, if this model is in "schemaful" mode, then any extraneous keys will be silently omitted.)

Note: For performance reasons, as of Sails v1.0 / Waterline 0.13, the valuesToSet object passed into this model method will be mutated in-place in most situations (whereas in Sails/Waterline v0.12, this was not necessarily the case).

Result
#
Type Description
Array? The updated records are not provided as a result by default, in order to optimize for performance. To override the default setting, chain .fetch() and an array of the updated records will be sent back. (Be aware that this requires an extra database query in some adapters.)
Errors
#
Name Type When?
UsageError Error Thrown if something invalid was passed in.
AdapterError Error Thrown if something went wrong in the database adapter. See Concepts > Models and ORM > Errors for an example of how to negotiate a uniqueness error (i.e. from attempting to update one or more records so that they violate a uniqueness constraint).
Error Error Thrown if anything else unexpected happens.

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

Meta keys
#
Key Type Details
fetch Boolean If set to true, then the array of updated records will be sent back.

Defaults to false.

For more information on meta keys, see .meta().

Example

#

To update a particular record, use .updateOne().

Or to update one or more records at the same time:

await User.update({ name:'Pen' })
.set({
  name:'Finn'
});

sails.log('Updated all users named Pen so that their new name is "Finn".  I hope they like it.');
Fetching updated records
#

To fetch updated records, use enable the fetch meta key:

var updatedUsers = await User.update({name:'Finn'})
.set({
  name:'Jake'
})
.fetch();

sails.log(`Updated all ${updatedUsers.length} user${updatedUsers.length===1?'':'s'} named "Finn" to have the name "Jake".  Here they are now:`);
sails.log(updatedUsers);

Notes

#

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference