Table.update()

Updates existing object in the object store with given changes

Syntax

table.update(key, changes)

Parameters

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

Return Value

Promise with the number of updated records (1 if an object that match the criteria was found and updated (regardless of whether the update affects 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 given changes to the object while put() will replace the entire object. Another difference is that in case key is not found, put() would create a new object while update() wont change anything. The returned Promise will NOT fail if key was not found but resolve with value 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 were 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, db.friends.update(2, {address: {zipcode: 12345}}) will replace the entire address object with {zipcode: 12345}. If your intent is to update the zipcode only, use dot notation as follows:

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

See Also

Collection.modify()

Table.put()

Table.bulkUpdate()

Table of Contents