.update()

Updates the provided key's value with the provided callback.

update(key, updateCallback)

Parameter

Description

key

Stringarrow-up-right

The target key

updateCallback

Callbackarrow-up-right

Function to call to update the data

Returns

Description

The updated data from the provided key's root

db.set('user', { name: 'Peter' }); // { name: 'Peter' }

db.update('user', (d) => d.name += "👨🏻‍💻"); // { name: 'Peter👨🏻‍💻' }

Also allows the use of dot notation:

db.get('user'); // { name: '5antos' }

db.update('user.name', (d) => d = d.toUpperCase()); // { name: '5ANTOS' }

And even a way to delete a key from the data:

db.get('person'); // { name: 'Peter', age: 19 }

db.update('person', (d) => delete d.age); // { name: 'Peter' } 

circle-info

When the callback parameter's value is not an object, the value that the update callback returns will be the value that will replace the old one, so it's optional to redefine its value inside the callback:

Last updated