# 更新更新更新

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

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

更改匹配的文档。仅操作员,且必须使用空过滤器。

Status: document database · free · one by default

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `collection` | string | 必填 | 哪个收藏 |
| `filter` | JSON text | 必填* | 文档。空需要 `all: true`. |
| `update` | JSON text | 必填 | 操作员 - `$set`, `$inc`, `$unset`, `$push`拒绝提供普通文件。 |
| `many` | boolean |  | `true` 默认值: 1。 |
| `all` | boolean |  | 需要接受空过滤器 。 |

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

## 拒绝整个文件 {#operators}

> **以通过文件取代文件、字段和所有**
>
> {活动:假 } 并不意味着“ 设置主动假 ” ——这意味着 ` 这是整个文件 , 所有其他领域都消失了。 拒绝是因为错误是沉默和不可逆转的。

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

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

## 默认发生一个, 许多人应请求 {#scope}

| 你写 | 触动 |
| --- | --- |
| 过滤器,无其他 | 第一局第一局 |
| 筛选器, 多个: 真实 | 每个匹配 |
| 空空过滤器 | 拒绝拒绝 |
| 空过滤器, 全部: 真实 | 收藏中的每一文档 |

默认的缺省在于保守地阅读模糊不清的请求。 `many: true` 当你说真的时, `matchedCount` 与你所期待的相悖。

```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` 和 `modifiedCount` 当文件已经具有了价值时,则有不同之处——匹配但未作更改,这并非失败。

- [删除删除删除](/zh/sdk/ref/connected-storage/run-write/delete): 过滤器的阴道
- [run()](/zh/sdk/ref/connected-storage/run): 行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动

```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']}")
```
