# guardar

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

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

Inserta un documento, o reemplaza el que tiene el mismo identificador. La escritura repetible.

Status: document database · free · insert or replace

## Parámetros {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `collection` | string | obligatorio | ¿Qué colección? |
| `data` | JSON text | obligatorio | El documento. `_id` reemplaza; sin, inserta. |

```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 qué esto en lugar de insertar {#idempotent}

|  | insertar | guardar |
| --- | --- | --- |
| Corre dos veces. | dos documentos | un documento |
| Necesita primero la identificación | no | sólo para sustituir |
| Bien por | eventos, registros, cualquier cosa que se añada | Estado que tiene una identidad |

> **Este es el que se puede alcanzar en un trabajo que podría funcionar dos veces**
>
> Una reintento, un Webhook reenviado, una importación reanudada — con insertar cada intento añade una fila. Con el ahorro, el segundo intento aterriza en el primero y nada se duplica.

## Reemplaza los campos que lleva {#merge}

El documento que pasa se establece sobre el existente. Los campos que deja fuera no se eliminan, pero los campos que incluye están sobrescritos, por lo que construya el objeto completo que quiere decir, en lugar de 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" } }) });
```

- [insertar](/es/sdk/ref/connected-storage/run-write/insert): Quand chaque Appel doit ajouter.
- [actualizar](/es/sdk/ref/connected-storage/run-write/update): Quand un seul champ cambio.
- [run()](/es/sdk/ref/connected-storage/run): Toutes les actions, les garde-fous et ce que chacune coûte.

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