.update()
Update all records matching criteria.
await Something.update(criteria)
.set(valuesToSet);
or
var updatedRecords = await Something.update(criteria).set(valuesToSet).fetch();
Argument | Type | Details | |
---|---|---|---|
1 | criteria | 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 | 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.) |
Type | Description |
---|---|
The created 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.) |
Name | Type | When? |
---|---|---|
UsageError | Thrown if something invalid was passed in. | |
AdapterError | 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 | Thrown if anything else unexpected happens. |
See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.
Key | Type | Details |
---|---|---|
fetch | 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().
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.');
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);
- This method can be used with
await
, promise chaining, or traditional Node callbacks.- This method can be used to replace an entire collection association (for example, a user’s list of friends), achieving the same result as the
replaceCollection
method. To modify items in a collection individually, use theaddToCollection
or removeFromCollection methods.