← Lessons

quiz vs the machine

Platinum1840

Databases

The Transactions in Document DB

Multi document atomic operations and when you need them.

6 min read · advanced · beat Platinum to climb

Atomicity Beyond One Document

A single document write in MongoDB is already atomic, meaning all of its changes apply or none do. For many designs, embedding related data in one document removes the need for anything more.

When You Need a Transaction

Sometimes a change must span multiple documents or collections and all parts must commit together. A multi document transaction gives you that all or nothing guarantee across documents.

  • Transactions provide ACID semantics across the documents they touch.
  • They commit or abort as a unit, so a failure leaves no partial change.
  • They are available on replica sets and sharded clusters.

Costs and Cautions

  • Transactions hold locks and resources, so long ones hurt throughput.
  • They have time limits and should stay short to avoid aborts.
  • On sharded clusters a transaction across shards is more expensive than within one.

Prefer a single document design first. Reach for a transaction only when truly cross document atomicity is required, and keep it brief.

Key idea

A single document write is atomic, and a multi document transaction extends all or nothing ACID semantics across documents and collections, but it costs throughput so keep it short and rare.

Check yourself

Answer to earn rating on the learn ladder.

1. What is already atomic in MongoDB without a transaction?

2. When should you reach for a multi document transaction?

3. Why keep transactions short?