Table.update()

Updates an existing object in the object store with the given changes

Syntax

table.update(key, changes)

Parameters

keyPrimary key
changesObject containing the key paths to each property you want to change.

Return Value

A Promise with the number of updated records (1 if an object that matches the criteria was found and updated, regardless of whether the update affected the object or not, otherwise 0). A result of 0 indicates that the given key was not found.

Remarks

Similar to SQL UPDATE. The difference between update() and put() is that update() will only apply the given changes to the object while put() will replace the entire object. Another difference is that if the key is not found, then put() will create a new object while update() will not change anything. The returned Promise will NOT fail if the key was not found but will resolve with a value of 0 instead of 1.

Equivalent to Table.where(":id").equals(key).modify(changes);

Sample

db.friends.update(2, {name: "Number 2"}).then(function (updated) {
  if (updated)
    console.log ("Friend number 2 was renamed to Number 2");
  else
    console.log ("Nothing was updated - there was no friend with primary key: 2");
});

Note: Be careful with nested object values. If you have an address field that includes city, state, and zipcode, then db.friends.update(2, {address: {zipcode: 12345}}) will replace the entire address object with {zipcode: 12345}. If you want to update the zipcode only, then use the dot notation as follows:

db.friends.update(friendId, {
  "address.zipcode": 12345
});

See Also

Collection.modify()

Table.put()

Table.bulkUpdate()

Table of Contents