1 · MongoDB · TRANSACTIONS
← Lesson 1: BEGIN … COMMIT
Atomicity and Transactions
A single operation on a document is atomic
A few documents, all or nothing
updateOne({ _id: "sword_dawn" }, {
$set: { "effects.0.damage_per_sec": 7,
base_price: 110 } })
// два поля, одна атомарная операция
// find → вычислить → update НЕ атомарно:
s = db.getMongo().startSession()
tx = s.getDatabase("game_db")
s.startTransaction()
tx.item_effects.updateOne(… burn …)
tx.item_configs.updateMany(… копии …)
s.commitTransaction() // или abort
LIMITATION
PRICE
ONE COLLECTION
I need a replica set
More expensive than a single entry
I need a transaction, too
Standalone is not supported. In the rs0 lab: transactions are supported, but there is no fault tolerance.
Resources, duration, conflicts. Embedding sometimes simplifies atomicity, if context and growth allow.
Two documents in the same collection are not updated as a single unit. `updateMany` is atomic for each document, not for the set.
Frequent transactions are more likely a design flaw. This is a good reason to review the requirements, document boundaries, and load.