Parameters
collectionstringrequiredWhich collection.
filterJSON textrequired*Which documents. Empty needs all: true.
updateJSON textrequiredOperators — $set, $inc, $unset, $push. A plain document is refused.
manybooleantrue updates every match. Default: one.
allbooleanRequired to accept an empty filter.
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}A whole document is refused
{ active: false } does not mean « set active to false » — it means « this is now the entire document », and every other field is gone. The refusal exists because that mistake is silent and irreversible.
// 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}})One by default, many on request
| You write | It touches |
|---|---|
| filter, nothing else | the first match |
| filter, many: true | every match |
| empty filter | refused |
| empty filter, all: true | every document in the collection |
The default of one is the conservative reading of an ambiguous request. Say many: true when you mean it — and read matchedCount against what you expected.
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 and modifiedCount differ when a document already had the value — matched but not changed. That is not a failure.