ejecutar · escribir

insertar

storage.run({ action: "insert", collection, documents })

Añade documentos a una colección. De uno a quinientos a la vez.

document databasefree1 to 500
Using Python?

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

Parámetros

collectionstringobligatorio

¿Qué colección?

documentsJSON textobligatorio

Una lista de 1 a 500 documentos, fuera de ese rango, se rechaza.

await storage.run({
  action: "insert",
  collection: "events",
  documents: JSON.stringify([
    { type: "signup", at: new Date().toISOString() },
    { type: "signup", at: new Date().toISOString() },
  ]),
});
// { insertedCount: 2, insertedIds: ["…", "…"] }
import json
from datetime import datetime, timezone

now = datetime.now(timezone.utc).isoformat()
storage.run(
    action="insert",
    collection="events",
    documents=json.dumps([
        {"type": "signup", "at": now},
        {"type": "signup", "at": now},
    ]),
)
# {"insertedCount": 2, "insertedIds": ["…", "…"]}

Más de quinientos

const SIZE = 500;
let inserted = 0;

for (let i = 0; i < all.length; i += SIZE) {
  const batch = all.slice(i, i + SIZE);
  const res = await storage.run({ action: "insert", collection,
    documents: JSON.stringify(batch) });
  inserted += res.insertedCount;
}
import json

SIZE = 500
inserted = 0

for i in range(0, len(rows), SIZE):
    batch = rows[i:i + SIZE]
    res = storage.run(action="insert", collection=collection, documents=json.dumps(batch))
    inserted += res["insertedCount"]
Un lote no es una transacción

Si una llamada en el bucle falla, las que se encuentran antes de aterrizar. Mantenga la cuenta y haga que la operación se pueda repetir: dé a cada documento un identificador propio y use el archivo de ahorro, si se ejecuta dos veces no debe duplicarse.

Se rechaza cuando el espacio está lleno

quota_exceeded, y se comprueba antes de la escritura. Eliminar todavía funciona más allá de la cuota, por lo que puede hacer espacio — leer uso antes de una gran importación en lugar de descubrirla a mitad de camino.