corse · scriver

Actualizzazione

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

Cambia documenti che coincide. Operatori solo, e un filtro vazio deve essere significat.

document databasefreeone by default
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.

filterJSON textobrigatório*

Quali documenti. Necessita vat all: true.

updateJSON textobrigatório

Operatori — $set, $inc, $unset, $push. Un documente plain è respinse.

manyboolean

true aggiorna ogni match. Predefinito: un.

allboolean

Obligatoriu di accettare un filtro vuoto.

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}

Un documentu intero è respinse

Passare un documente lo sostituirà, campi e tot

{ active: false } non significa « set active to false » — significa « questo è ora l'intero documento » e ogni altro campo è partito. Il renegazione esiste perché quel erro è silente e irreversibile.

// 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}})

Uno per default, molti su richiesta

Tu scriviToca
Filtro, ningun altrola prima partida
filtri, molti: veritCada fèrs
Filtro vazio- Negati
Filtro vuoto, tutto: veroCada documento della collezion

Il predefinit di una è la lectura conservatrice di una richiesta ambigua. Di many: true quando lo si dice — e lee matchedCount a contra di ciò che si aspettava.

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 e modifiedCount differiscono quando un documento aveva già il valore — igual ma non cambiato.