Bird
Raised Fist0
MongoDBquery~20 mins

Write concern levels (w: 1, majority) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Write Concern Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What does write concern w:1 guarantee?

In MongoDB, if you set the write concern to { w: 1 }, what does it guarantee about the write operation?

AThe write is not acknowledged at all.
BThe write is acknowledged by all nodes in the replica set.
CThe write is acknowledged only after majority of nodes confirm.
DThe write is acknowledged by the primary node only.
Attempts:
2 left
💡 Hint

Think about which node confirms the write first in a replica set.

❓ query_result
intermediate
2:00remaining
What does write concern w: 'majority' guarantee?

In MongoDB, if you set the write concern to { w: 'majority' }, what does it guarantee about the write operation?

AThe write is acknowledged by a majority of voting nodes in the replica set.
BThe write is acknowledged by the primary node only.
CThe write is acknowledged by all nodes including arbiters.
DThe write is acknowledged only after all secondaries confirm.
Attempts:
2 left
💡 Hint

Majority means more than half of voting nodes.

📝 Syntax
advanced
2:00remaining
Which write concern syntax is correct to wait for majority?

Choose the correct MongoDB write concern syntax to ensure the write is acknowledged by the majority of replica set members.

A{ w: 'majority' }
B{ w: majority }
C{ w: 0, j: true }
D{ w: 1, j: false }
Attempts:
2 left
💡 Hint

Remember that 'majority' is a string value in write concern.

🧠 Conceptual
advanced
2:00remaining
Why might you choose write concern w:1 over w:'majority'?

Which reason best explains why a developer might choose { w: 1 } instead of { w: 'majority' } for write concern?

ATo guarantee writes are journaled on disk before acknowledgment.
BTo ensure data is written to all nodes before continuing.
CTo get faster write acknowledgments with less waiting.
DTo avoid any acknowledgment and improve performance.
Attempts:
2 left
💡 Hint

Think about speed versus durability trade-offs.

🔧 Debug
expert
2:00remaining
What error occurs with invalid write concern value?

Consider this MongoDB write operation with write concern:

{ w: 2 }

Assuming the replica set has only 3 nodes, what error or behavior will occur?

AThe write succeeds immediately without waiting for any nodes.
BThe write waits for acknowledgment from exactly 2 nodes and succeeds if they respond.
CThe write fails with <code>WriteConcernError</code> because w:2 is invalid for this replica set.
DThe write causes a syntax error and does not run.
Attempts:
2 left
💡 Hint

Write concern w can be a number specifying how many nodes must acknowledge.

Practice

(1/5)
1. What does the write concern w: 1 mean in MongoDB?
easy
A. The write is acknowledged by all servers in the cluster.
B. The write is acknowledged by only one server before returning success.
C. The write is acknowledged by the majority of servers before returning success.
D. The write is not acknowledged by any server.

Solution

  1. Step 1: Understand write concern w: 1

    This means the write operation waits for acknowledgment from only one server.
  2. Step 2: Compare with other write concerns

    w: majority waits for most servers, so w: 1 is less strict and faster.
  3. Final Answer:

    The write is acknowledged by only one server before returning success. -> Option B
  4. Quick Check:

    Write concern w: 1 = one server acknowledgment [OK]
Hint: Remember: w:1 means wait for one server only [OK]
Common Mistakes:
  • Confusing w:1 with w:majority
  • Thinking w:1 waits for all servers
  • Assuming w:1 means no acknowledgment
2. Which of the following is the correct syntax to set write concern to majority in a MongoDB write operation?
easy
A. { writeConcern: { w: majority } }
B. { writeConcern: { w: "1" } }
C. { writeConcern: { w: majority'' } }
D. { writeConcern: { w: 'majority' } }

Solution

  1. Step 1: Identify correct syntax for string values

    Write concern values like 'majority' must be strings enclosed in quotes.
  2. Step 2: Check each option

    { writeConcern: { w: 'majority' } } uses quotes correctly. { writeConcern: { w: majority } } misses quotes. { writeConcern: { w: majority'' } } has syntax error with extra quotes. { writeConcern: { w: "1" } } sets w to string '1', not 'majority'.
  3. Final Answer:

    { writeConcern: { w: 'majority' } } -> Option D
  4. Quick Check:

    Use quotes for string write concern values [OK]
Hint: Write concern strings need quotes like 'majority' [OK]
Common Mistakes:
  • Omitting quotes around 'majority'
  • Using invalid quotes or syntax
  • Confusing w:1 string with w:'majority'
3. Consider this MongoDB write operation with write concern:
db.collection.insertOne({name: 'Alice'}, {writeConcern: {w: 'majority'}})

What does this guarantee about the write?
medium
A. The write is confirmed by the majority of replica set members before success.
B. The write is not confirmed by any server before success.
C. The write is confirmed by at least one server before success.
D. The write is confirmed by all servers including secondaries and arbiters.

Solution

  1. Step 1: Understand w: 'majority' write concern

    This means the write waits for acknowledgment from most members of the replica set.
  2. Step 2: Compare with other options

    The write is confirmed by at least one server before success. is w:1. The write is not confirmed by any server before success. means no acknowledgment. The write is confirmed by all servers including secondaries and arbiters. is incorrect because arbiters do not store data and are not counted.
  3. Final Answer:

    The write is confirmed by the majority of replica set members before success. -> Option A
  4. Quick Check:

    w: 'majority' means majority confirmation [OK]
Hint: Majority means most data-bearing members confirm write [OK]
Common Mistakes:
  • Thinking arbiters count towards majority
  • Confusing majority with all servers
  • Assuming w:majority is same as w:1
4. You wrote this code:
db.collection.insertOne({item: 'book'}, {writeConcern: {w: majority}})

But it throws an error. What is the problem?
medium
A. The write concern value majority must be a string in quotes.
B. The writeConcern option is misspelled.
C. The insertOne method does not accept writeConcern option.
D. The document to insert is missing required fields.

Solution

  1. Step 1: Check syntax of write concern value

    The value majority must be a string, so it needs quotes: 'majority'.
  2. Step 2: Verify other parts

    writeConcern is spelled correctly, insertOne accepts writeConcern, and document is valid.
  3. Final Answer:

    The write concern value majority must be a string in quotes. -> Option A
  4. Quick Check:

    Write concern strings need quotes [OK]
Hint: Always quote string write concern values like 'majority' [OK]
Common Mistakes:
  • Forgetting quotes around 'majority'
  • Assuming writeConcern is misspelled
  • Thinking insertOne disallows writeConcern
5. You want to ensure your MongoDB writes are safe and confirmed by most servers, but you also want to avoid waiting for all servers. Which write concern should you choose and why?
hard
A. Use w: 1 because it waits for only one server, making writes fastest.
B. Use w: 0 because it does not wait, making writes fastest but unsafe.
C. Use w: majority because it waits for most servers, balancing safety and speed.
D. Use w: all because it waits for every server, ensuring maximum safety.

Solution

  1. Step 1: Understand the trade-offs

    w: 1 is fast but less safe. w: majority waits for most servers, giving good safety without waiting for all.
  2. Step 2: Evaluate options

    w: all waits for every server, which can be slow. w: 0 is unsafe as it doesn't wait for acknowledgment.
  3. Final Answer:

    Use w: majority because it waits for most servers, balancing safety and speed. -> Option C
  4. Quick Check:

    Majority write concern balances safety and speed [OK]
Hint: Majority write concern balances safety and speed well [OK]
Common Mistakes:
  • Choosing w:1 for safety
  • Assuming w:all is always best
  • Ignoring performance impact of write concern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard