Parameter
collectionstringerforderlichWelche Sammlung.
filterJSON texterforderlich*Welche Dokumente. Leere braucht all: true.
updateJSON texterforderlichBetreiber — $set, $inc, $unset, $push. Ein einfaches Dokument wird abgelehnt.
manybooleantrue aktualisiert jedes Match. Standard: eins.
allbooleanErforderlich, 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
{ 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 schreibst | Es berührt |
|---|---|
| Filter, sonst nichts | das erste Spiel |
| Filter, viele: wahr | jedes Match |
| leerer Filter | abgelehnt |
| leerer Filter, alles: true | jedes 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.