# update

Canonical URL: https://developers.iagenify.com/sdk/ref/connected-storage/run-write/update/
Language: en

`storage.run({ action: "update", collection, filter, update, many, all })`

Changes documents that match. Operators only, and an empty filter has to be meant.

Status: document database · free · one by default

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `collection` | string | required | Which collection. |
| `filter` | JSON text | required* | Which documents. Empty needs `all: true`. |
| `update` | JSON text | required | Operators — `$set`, `$inc`, `$unset`, `$push`. A plain document is refused. |
| `many` | boolean |  | `true` updates every match. Default: one. |
| `all` | boolean |  | Required to accept an empty filter. |

```javascript
await storage.run({
  action: "update",
  collection: "users",
  filter: JSON.stringify({ email: "a@example.com" }),
  update: JSON.stringify({ $set: { active: false }, $inc: { logins: 1 } }),
});
// { matchedCount: 1, modifiedCount: 1 }
```

## A whole document is refused {#operators}

> **Passing a document would replace it, fields and all**
>
> { active: false } does not mean « set active to false » — it means « this is now the entire document », and every other field is gone. The refusal exists because that mistake is silent and irreversible.

```javascript
// Refused
update: JSON.stringify({ active: false })

// Meant
update: JSON.stringify({ $set: { active: false } })
```

## One by default, many on request {#scope}

| You write | It touches |
| --- | --- |
| filter, nothing else | the first match |
| filter, many: true | every match |
| empty filter | refused |
| empty filter, all: true | every document in the collection |

The default of one is the conservative reading of an ambiguous request. Say `many: true` when you mean it — and read `matchedCount` against what you expected.

```javascript
const res = await storage.run({ action: "update", collection, filter, update, many: true });

if (res.matchedCount !== expected) {
  console.warn(`expected ${expected}, matched ${res.matchedCount}`);
}
```

`matchedCount` and `modifiedCount` differ when a document already had the value — matched but not changed. That is not a failure.

- [delete](/sdk/ref/connected-storage/run-write/delete): Le même garde-fou sur le filtre vide.
- [run()](/sdk/ref/connected-storage/run): Toutes les actions, les garde-fous et ce que chacune coûte.

```python
import json

storage.run(
    action="update",
    collection="users",
    filter=json.dumps({"email": "a@example.com"}),
    update=json.dumps({"$set": {"active": False}, "$inc": {"logins": 1}}),
)
# {"matchedCount": 1, "modifiedCount": 1}
```

```python
# Refused
update = json.dumps({"active": False})

# Meant
update = json.dumps({"$set": {"active": False}})
```

```python
res = storage.run(action="update", collection=collection, filter=filter, update=update, many=True)

if res["matchedCount"] != expected:
    print(f"expected {expected}, matched {res['matchedCount']}")
```
