run · schreiben

Aktualisierung

storage.run({ action: "update", collection, filter, update, many, all })

Ändert Dokumente, die übereinstimmen. Nur Operatoren und ein leerer Filter müssen gemeint sein.

document databasefreeone by default
Using Python?

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

Parameter

collectionstringerforderlich

Welche Sammlung.

filterJSON texterforderlich*

Welche Dokumente. Leere braucht all: true.

updateJSON texterforderlich

Betreiber — $set, $inc, $unset, $push. Ein einfaches Dokument wird abgelehnt.

manyboolean

true aktualisiert jedes Match. Standard: eins.

allboolean

Erforderlich, um einen leeren Filter zu akzeptieren.

await storage.run({
  action: "update",
  collection: "users",
  filter: JSON.stringify({ email: "a@example.com" }),
  update: JSON.stringify({ $set: { active: false }, $inc: { logins: 1 } }),
});
// { matchedCount: 1, modifiedCount: 1 }
import json

storage.run(
    action="update",
    collection="users",
    filter=json.dumps({"email": "a@example.com"}),
    update=json.dumps({"$set": {"active": False}, "$inc": {"logins": 1}}),
)
# {"matchedCount": 1, "modifiedCount": 1}

Ein ganzes Dokument wird abgelehnt

Ein Dokument zu übergeben würde es ersetzen, Felder und alle

{ aktiv: false } bedeutet nicht, dass "aktiv auf false gesetzt" wird — es bedeutet "das ist jetzt das gesamte Dokument", und jedes andere Feld ist weg. Die Ablehnung existiert, weil dieser Fehler still und unumkehrbar ist.

// Refused
update: JSON.stringify({ active: false })

// Meant
update: JSON.stringify({ $set: { active: false } })
# Refused
update = json.dumps({"active": False})

# Meant
update = json.dumps({"$set": {"active": False}})

Eine Standardeinstellung, viele auf Anfrage

Du schreibstEs berührt
Filter, sonst nichtsdas erste Spiel
Filter, viele: wahrjedes Match
leerer Filterabgelehnt
leerer Filter, alles: truejedes Dokument in der Sammlung

Der Standardwert von einem ist die konservative Lesung einer mehrdeutigen Anfrage. many: true wenn du es ernst meinst — und liest matchedCount gegen das, was du erwartet hast.

const res = await storage.run({ action: "update", collection, filter, update, many: true });

if (res.matchedCount !== expected) {
  console.warn(`expected ${expected}, matched ${res.matchedCount}`);
}
res = storage.run(action="update", collection=collection, filter=filter, update=update, many=True)

if res["matchedCount"] != expected:
    print(f"expected {expected}, matched {res['matchedCount']}")

matchedCount und modifiedCount unterscheiden, wenn ein Dokument bereits den Wert hatte — zusammengepaßt, aber nicht geändert. Das ist kein Fehler.