Write concern tells MongoDB how sure you want to be that your data is saved safely. It helps protect your data from being lost.
Write concern and data durability in MongoDB
db.collection.insertOne(document, { writeConcern: { w: <level>, j: <boolean>, wtimeout: <ms> } })w sets how many servers must confirm the write (e.g., 1, majority, or 0).
j means waiting for the write to be saved to the journal (disk).
db.users.insertOne({name: 'Alice'}, { writeConcern: { w: 1 } })db.orders.insertOne({item: 'Book'}, { writeConcern: { w: 'majority', j: true } })db.logs.insertOne({event: 'login'}, { writeConcern: { w: 0 } })This command inserts a document into the inventory collection. It waits for the majority of servers to confirm the write, ensures the write is saved to the journal (disk), and times out if it takes more than 5 seconds.
db.inventory.insertOne(
{ item: 'pen', qty: 50 },
{ writeConcern: { w: 'majority', j: true, wtimeout: 5000 } }
)Using w: 'majority' helps keep data safe by waiting for most servers to confirm.
Setting j: true ensures data is written to disk journal, improving durability.
Higher write concern can slow down writes but protects data better.
Write concern controls how MongoDB confirms data is saved.
It balances speed and safety for your data.
Use write concern options to match your app's needs for durability.
Practice
writeConcern option in MongoDB control?Solution
Step 1: Understand the role of write concern
Write concern tells MongoDB how to confirm that a write operation is saved and durable.Step 2: Differentiate from other options
It does not control data format, retrieval speed, or document size.Final Answer:
How MongoDB confirms that data is saved to the database -> Option AQuick Check:
Write concern = data save confirmation [OK]
- Confusing write concern with data format
- Thinking it controls read speed
- Assuming it limits document size
Solution
Step 1: Recall write concern values
w:1 means wait for acknowledgment from the primary node only.Step 2: Check other options
w:0 means no acknowledgment; w:'majority' waits for majority nodes; w:-1 is invalid.Final Answer:
{ writeConcern: { w: 1 } } -> Option DQuick Check:
w:1 = primary ack wait [OK]
- Using w:0 which means no acknowledgment
- Confusing 'majority' with primary only
- Using invalid negative values
db.collection.insertOne({name: 'Alice'}, {writeConcern: {w: 'majority', wtimeout: 5000}})What happens if the majority of nodes do not acknowledge the write within 5 seconds?
Solution
Step 1: Understand w:'majority' and wtimeout
The write waits for majority nodes to confirm within 5000 ms (5 seconds).Step 2: Outcome if timeout exceeded
If majority nodes don't respond in time, MongoDB returns a timeout error and the write is considered failed.Final Answer:
The write operation fails with a timeout error -> Option CQuick Check:
w:'majority' + wtimeout = fail if no timely ack [OK]
- Assuming write succeeds immediately
- Thinking MongoDB retries forever
- Ignoring write concern on timeout
db.collection.insertOne({item: 'book'}, {writeConcern: {w: 2, wtimeout: '3000'}})But it throws an error. What is the problem?
Solution
Step 1: Check writeConcern syntax
wtimeout expects a number (milliseconds), not a string.Step 2: Validate other parts
w:2 is valid if the replica set has at least 2 nodes; insertOne accepts writeConcern; 'item' field is valid.Final Answer:
wtimeout must be a number, not a string -> Option AQuick Check:
wtimeout type must be number [OK]
- Using string for wtimeout
- Thinking w:2 is invalid always
- Assuming insertOne disallows writeConcern
Solution
Step 1: Understand w and j options
w:3 means wait for acknowledgment from primary plus two secondaries (total 3 nodes). j:true means wait for write to be committed to journal (disk) on primary.Step 2: Check other options
B waits only for primary with journaling; C waits for majority but no journaling; D waits for 2 nodes but no journaling.Final Answer:
{ writeConcern: { w: 3, j: true } } -> Option BQuick Check:
w:3 + j:true = 3 nodes + disk write [OK]
- Ignoring journaling for durability
- Using w:'majority' without knowing node count
- Assuming w:2 means two secondaries only
Ready to prove it?
Pass the test and this pattern moves unknown
