# Salva

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

`storage.run({ action: "save", collection, data })`

Inserisce un document, o sostituisce il con il medesimo identificator.

Status: document database · free · insert or replace

## Parâmetros {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `collection` | string | obrigatório | Quale collezion. |
| `data` | JSON text | obrigatório | Con un `_id` il documento lo sostituisce; senza, inserisce. |

```javascript
// No _id: inserted
await storage.run({ action: "save", collection: "profiles",
  data: JSON.stringify({ email: "a@example.com", plan: "go" }) });
// { ok: true, id: "66f0…" }

// With _id: replaced, or created if it is not there
await storage.run({ action: "save", collection: "profiles",
  data: JSON.stringify({ _id: "66f0…", email: "a@example.com", plan: "starter" }) });
// { ok: true, id: "66f0…", modifiedCount: 1 }
```

## Por qua esto e non insira {#idempotent}

|  | Inserire | Salva |
| --- | --- | --- |
| Correr due volte | 2 documenti | un documente |
| Necessita prima l'identit | non | solo per sostituir |
| Buone per | eventi, logs, qualsiasi cosa anex-solo | istat identita |

> **É questo a percernîr in un lavoro che potrebbe zarpar due volte**
>
> Un tentazion, un webhook relivered, un importare ripreso — con insere cada tentato aggiunge una fila. Con save, il secondo tentato aterrisce sul primo e nulla è duplicat.

## Soplaza i campi che porta {#merge}

Il documenti che passate è impostat su quello existente. I campi che olvidi non sono remoti, ma i campi che includete sono sovrascrits — quindi costruite l'oggetto intero che si intende, piuttosto que un fragmento.

```javascript
// To change one field and leave the rest alone, update is the clearer tool
await storage.run({ action: "update", collection: "profiles",
  filter: JSON.stringify({ _id: id }),
  update: JSON.stringify({ $set: { plan: "starter" } }) });
```

- [Inserire](/pt/sdk/ref/connected-storage/run-write/insert): Quant chaque appel doit jutter.
- [Actualizzazione](/pt/sdk/ref/connected-storage/run-write/update): Quant un suul champ change.
- [run()](/pt/sdk/ref/connected-storage/run): Tutes les actions, les garde-fous et ce que chaque coute.

```python
import json

# No _id: inserted
storage.run(action="save", collection="profiles",
            data=json.dumps({"email": "a@example.com", "plan": "go"}))
# {"ok": True, "id": "66f0…"}

# With _id: replaced, or created if it is not there
storage.run(action="save", collection="profiles",
            data=json.dumps({"_id": "66f0…", "email": "a@example.com", "plan": "starter"}))
# {"ok": True, "id": "66f0…", "modifiedCount": 1}
```

```python
import json

# To change one field and leave the rest alone, update is the clearer tool
storage.run(action="update", collection="profiles",
            filter=json.dumps({"_id": profile_id}),
            update=json.dumps({"$set": {"plan": "starter"}}))
```
