run · write

save

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

Inserts a document, or replaces the one with the same identifier. The repeatable write.

document databasefreeinsert or replace
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Parameters

collectionstringrequired

Which collection.

dataJSON textrequired

The document. With an _id it replaces; without, it inserts.

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

Why this rather than insert

insertsave
Run twicetwo documentsone document
Needs the id firstnoonly to replace
Good forevents, logs, anything append-onlystate that has an identity
This is the one to reach for in a job that might run twice

A retry, a redelivered webhook, a resumed import — with insert each attempt adds a row. With save, the second attempt lands on the first and nothing is duplicated.

It replaces the fields it carries

The document you pass is set over the existing one. Fields you leave out are not removed, but fields you include are overwritten — so build the whole object you mean, rather than a fragment.

// 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" } }) });
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"}}))