MongoDB Foundations·Documents with discipline

Transactions and consistency

Group changes that must all succeed or all fail, and choose how durable a write must be.

Beginner 12 min read Lesson 3 of 300 Challenge included
What you will be able to do
  • Explain why a replica set is needed for transactions.
  • Open a session with startSession and commit work with withTransaction.
  • Choose a write concern such as majority for durability.
  • Describe what read concern controls.

In plain English

A replica set is several MongoDB servers keeping the same data so the system keeps working if one fails. A session, opened with startSession, is a way to run commands that belong together. withTransaction wraps a block of work so that either every change commits or none of them do. A write concern says how many copies must acknowledge a write before it is reported as successful, and a read concern says which state of the data you are willing to read. Multi-document transactions only work on a replica set, so a single standalone server cannot run them.

Step by step

  1. 1Confirm you are connected to a replica set rather than a standalone server.
  2. 2Open a session with db.getMongo().startSession().
  3. 3Run several updates inside session.withTransaction.
  4. 4Close the session with endSession when you are done.
  5. 5Set writeConcern to majority for writes that must survive a crash.
transfer.jsJavaScript
const session = db.getMongo().startSession()

session.withTransaction(async () => {
  const accounts = session.getDatabase("bank").accounts
  await accounts.updateOne(
    { _id: "ada" },
    { $inc: { balance: -100 } }
  )
  await accounts.updateOne(
    { _id: "grace" },
    { $inc: { balance: 100 } }
  )
})

session.endSession()

db.accounts.updateOne(
  { _id: "ada" },
  { $inc: { balance: -10 } },
  { writeConcern: { w: "majority" } }
)

Common mistakes

  • Running a multi-document transaction against a standalone server fails because transactions require a replica set.
  • Doing slow network or human work inside a transaction holds locks and blocks other operations.
  • Assuming a write is safely stored just because it was acknowledged, without setting a stronger write concern, is unsafe.
  • Ending a session without committing or aborting the transaction leaves the session hanging in an uncertain state.
Your turn

Move money atomically

Medium

Open a session and transfer 50 from account "ada" to account "grace" inside withTransaction so both updates commit or neither does.

JAVASCRIPT · starter
const session = db.getMongo().startSession()
Check your understanding before moving on.
Knowledge checkQuestion 1

What does a write concern of "majority" guarantee?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Performance and explain
Lesson 3 of 300 · 0% of course