.update()

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

update(key, updateCallback)

Parameter

Description

key

String

The target key

updateCallback

Callback

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' } 

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:

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

db.update('user.name', (d) => d = d.toLowerCase()); // { name: '5antos' }
// is the same as
db.update('user.name', (d) => d.toLowerCase()); // { name: '5antos' }

Last updated