corse · scriver

Inserire

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

Aggiunge documenti a una collezion. Un a quinquacentos per volta.

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

collectionstringobrigatório

Quale collezion.

documentsJSON textobrigatório

Una lista di 1 a 500 documenti.

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": ["…", "…"]}

Piu de quinq cent

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 lotto non è una transaccion

Se una chiamata nel loop fail, quella prima di aterrì. Mantene la conta, e rendere l'operazion repetibile — dad a ogni documente un identificatore del vostro e usate save in lugar, se correre due volte non deve duplicare.

Se nega quando l'espazio è pieno

quota_exceeded, e è verificat prima de la scrittura. Eliminando ancora funciona oltre la quota, percioè si puè fare loc — leg usage prima di un'important grande, invece di scopri-lo a midjme.