MongoDB Write Concern

Intermediate
7 min read
Updated: Nov 2026
Getting Started

What Is Write Concern?

A write concern specifies how much acknowledgement MongoDB must give before reporting a write as successful. The w option sets how many replica set members must have applied the write: w: 1 means just the primary, and w: "majority" means a majority of data-bearing voting members. Stronger concerns make writes durable across failover, with a bit more latency.

Why It Matters

ReasonBenefit
Durability controlChoose how safe a write must be before your code continues.
Survive failovermajority writes are preserved when a new primary is elected.
Tune latencyUse w: 1 for low-value data like analytics counters.
Clear failure signalwtimeout tells you when acknowledgement took too long instead of waiting forever.

How To Use It

  1. Connect to a replica set and check the current defaults with db.adminCommand({ getDefaultRWConcern: 1 }).
  2. Insert a document with the default write concern and note the acknowledged result.
  3. Insert again with { writeConcern: { w: "majority" } } for important data.
  4. Add wtimeout so the application does not hang if members are down.
  5. Add j: true if you need the write committed to the journal.
  6. Handle write concern errors in your application, since the write may still have been applied.
  7. Set a default in the connection string or deployment when most writes need the same level.

mongosh Examples

mongosh
// Default write concern for the deployment
db.adminCommand({ getDefaultRWConcern: 1 })

// Acknowledged by the primary only
db.orders.insertOne({ item: "pen", qty: 1 }, { writeConcern: { w: 1 } })

// Acknowledged by a majority, with a 5 second limit
db.orders.insertOne(
  { item: "book", qty: 2 },
  { writeConcern: { w: "majority", wtimeout: 5000 } }
)

// Majority and journaled
db.orders.updateOne(
  { item: "book" },
  { $set: { qty: 3 } },
  { writeConcern: { w: "majority", j: true } }
)

// Connection string option form
// mongodb://host1,host2,host3/shop?replicaSet=rs0&w=majority&wtimeoutMS=5000

Edge Cases & Pitfalls

wtimeout errors do not undo the write

A timeout means acknowledgement did not arrive in time, not that the write failed. The write may still replicate.

w: 0 hides errors

Unacknowledged writes give no confirmation, so failures go unnoticed.

Requested members unavailable

With w equal to a number or majority, if too many members are down, the write waits and may time out.

Defaults vary by version and topology

Do not assume the default; check getDefaultRWConcern or set it explicitly.

Cheat Sheet

TopicDetail
w: 1Acknowledged by the primary only (common older default)
w: "majority"Acknowledged by a majority of data-bearing voting members; default in recent versions for most deployments
w: 0No acknowledgement; fire and forget, not recommended
w: N (a number)Acknowledged by that many members
j: trueWait for the write to be written to the on-disk journal
wtimeoutMilliseconds to wait for acknowledgement before returning an error
Per operation{ writeConcern: { w: "majority", wtimeout: 5000 } } in options
Connection string?w=majority&wtimeoutMS=5000
Cluster defaultsetDefaultRWConcern sets deployment-wide defaults

Key Takeaways

w: 1 is fast but a write acknowledged only by the primary can be lost if the primary fails before replicating. Use w: "majority" for data you cannot afford to lose.

Frequently Asked Questions

What is write concern in MongoDB?

It controls how many replica set members must acknowledge a write before the operation is reported successful.

What does w: majority mean?

The write must be applied on a majority of data-bearing voting members, so it survives a primary failover.

What is the difference between w: 1 and w: majority?

w: 1 acknowledges after the primary applies the write. majority also waits for replication to most members, which is safer but a little slower.

What does j: true do?

It waits until the write is recorded in the on-disk journal on the acknowledging members.

What is wtimeout?

A time limit in milliseconds for the write concern to be satisfied. If exceeded, the operation returns a write concern error.

Does write concern work on a standalone server?

Only w: 1 and w: 0 are meaningful there, since there are no other members to acknowledge.

Next: MongoDB Oplog

Continue the MongoDB learning path.

MongoDB Oplog →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful