Skip to the content
hinny

Guides

Update documents

Replace a note, change individual fields, or read a consistent view across a batch of writes.

Change a field

Continue with the quickstart database. Use upsert() to replace the whole document at a key. To change just its kind while keeping its text, patch the attribute.

Python
changed = docs.patch(1, attrs={"kind": "done"})
print(changed, docs.text(1)["body"])  # True Renew the apartment lease

Write an atomic batch

Every write is an atomic batch with a monotonic sequence number. Queries read an immutable view and never block writers. snapshot() pins a view for as long as you hold it. One process writes at a time; others can open read-only snapshots.

Read the same key before and after the batch with snapshot().

Python
before = docs.snapshot()
docs.apply(upsert=[{"key": 7, "text": "New task"}], delete=[2])
print(before.text(2)["body"])  # Bring the bicycle indoors
print(docs.text(2))            # None