> For the complete documentation index, see [llms.txt](https://simpldb.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://simpldb.js.org/documentation/collection/update.md).

# .update()

Updates the entries that match the provided filter with the provided callback. Updates all the entries if no filter is provided.

### update(updateCallback, filter)

| **Parameter**  | **Description**                                                                                                                                                                                                                                                         |
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| updateCallback | <p><a href="https://developer.mozilla.org/en-US/docs/Glossary/Callback_function">Callback</a></p><p>Function to run for each entry returned by the filter</p>                                                                                                           |
| filter         | <p><a href="https://developer.mozilla.org/en-US/docs/Glossary/Callback_function">Callback</a> (optional)</p><p>Function to test each element of the array. Return a value that coerces to <code>true</code> to keep the element, or to <code>false</code> otherwise</p> |

| **Returns**                                                                                                                                                        | **Description**     |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------- |
| [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Data](https://simpldb.gitbook.io/docs/types-and-interfaces/data)> | The updated entries |

```javascript
// Note: People is a collection.

People.getAll();  // [{ id: 1, name: 'Peter', salary: 1200 }, { id: 2, name: 'Michael', salary: 900 }] 

// Updating a value from all matching entries
People.update(
  person => person.salary += 100,
  target => target.salary < 1000
); // [{ id: 2, name: 'Michael', salary: 1000 }] 

// Deleting the 'name' key from all matching entries
People.update(
  person => person.name = undefined,
  target => target.salary === 1000
); // [{ id: 2, salary: 1000 }]

// Updating all entries (by omitting the filter)
People.update(person => person.salary += 500); // [{ id: 1, name: 'Peter', salary: 1700 }, { id: 2, salary: 1500 }] 

// Updating multiple values from all entries (by omitting the filter)
People.update(
  person => {
    if (!person.name) person.name = 'John';
  
    if (person.id === 1)
      person.salary += 300;
    else
      person.salary += 150;
      
    person.isChief = person.id === 1;
  }
); // [{ id: 1, name: 'Peter', salary: 2000, isChief: true } , { id: 2, name: 'John', salary: 1650, isChief: false }] 
```

{% hint style="info" %}
A new way of updating an entry's data from a collection was introduced in version 2.11.0.

Now, instead of using the **Collection#update** method, you can simply get or fetch the entry's data with **Collection#get**, **Collection#fetch**, **Collection#getOrCreate** or **Collection#fetchOrCreate**, update a property directly in the code and then use the new [`Data`](/types-and-interfaces/data.md)`#save` method.

See the examples below:
{% endhint %}

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const user = Users.get(u => u.name === 'Peter');

user.age = 20;
user.save();
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
import { Modifiable } from 'simpl.db';

const user = <Modifiable<User>> Users.get(u => u.name === 'Peter');
// or
const user = Users.get(u => u.name === 'Peter') as Modifiable<User>;

user.age = 20;
user.save();
```

{% endtab %}
{% endtabs %}
