Bird
Raised Fist0
MongoDBquery~20 mins

Write concern basics in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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 is the effect of write concern { w: 0 }?

Consider a MongoDB insert operation with the write concern set to { w: 0 }. What will be the behavior of this operation?

AThe operation returns immediately without waiting for any acknowledgment from the server.
BThe operation waits for acknowledgment from the primary only.
CThe operation waits for acknowledgment from all replica set members.
DThe operation fails immediately with an error.
Attempts:
2 left
💡 Hint

Think about what w: 0 means in terms of server acknowledgment.

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 this guarantee about the write operation?

AThe write is acknowledged after being written to all members, including arbiters.
BThe write is acknowledged only by the primary node.
CThe write is acknowledged immediately without waiting.
DThe write is acknowledged after being written to a majority of voting replica set members.
Attempts:
2 left
💡 Hint

Consider what 'majority' means in a replica set context.

📝 Syntax
advanced
2:00remaining
Identify the invalid write concern option

Which of the following write concern options is invalid and will cause a syntax or runtime error in MongoDB?

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

Check the valid range and types for the w option.

🧠 Conceptual
advanced
2:00remaining
What happens if write concern timeout is reached?

If a write operation uses a write concern with a wtimeout set, and the timeout is reached before the write is acknowledged, what is the outcome?

AThe write operation waits indefinitely until acknowledged.
BThe write operation is aborted and rolled back automatically.
CThe write operation completes but the client receives a timeout error.
DThe write operation is acknowledged immediately ignoring the timeout.
Attempts:
2 left
💡 Hint

Think about what a timeout means for client acknowledgment.

🔧 Debug
expert
3:00remaining
Why does this write concern cause a write failure?

A MongoDB write operation uses the write concern { w: 3, wtimeout: 1000 } on a replica set with only 2 voting members. What error will occur and why?

AThe operation fails with a write concern error because the requested w is higher than the number of voting members.
BThe operation succeeds immediately because wtimeout is set to 1000 ms.
CThe operation fails with a syntax error due to invalid wtimeout value.
DThe operation ignores the write concern and writes only to the primary.
Attempts:
2 left
💡 Hint

Consider the relationship between w and the number of voting members in the replica set.

Practice

(1/5)
1.

What does write concern in MongoDB control?

easy
A. The size of the database files
B. The speed of reading data from the database
C. The number of users connected to the database
D. How sure MongoDB is that your data is saved

Solution

  1. Step 1: Understand the role of write concern

    Write concern defines the level of acknowledgment requested from MongoDB when writing data.
  2. Step 2: Identify what write concern controls

    It controls how sure the database is that the data has been saved successfully.
  3. Final Answer:

    How sure MongoDB is that your data is saved -> Option D
  4. Quick Check:

    Write concern = Data save confirmation [OK]
Hint: Write concern = data save confirmation level [OK]
Common Mistakes:
  • Confusing write concern with read speed
  • Thinking it controls database size
  • Assuming it manages user connections
2.

Which of the following is the correct way to set a write concern of w: 1 in a MongoDB insert operation?

db.collection.insertOne({name: 'Alice'}, {writeConcern: ???})
easy
A. {w: 1}
B. {w: 'majority'}
C. {w: 0}
D. {w: true}

Solution

  1. Step 1: Recall write concern syntax

    Write concern is set as an object with key w and a value indicating the level.
  2. Step 2: Match the correct value for w: 1

    The correct syntax is {w: 1} to wait for acknowledgment from one server.
  3. Final Answer:

    {w: 1} -> Option A
  4. Quick Check:

    Write concern syntax = {w: 1} [OK]
Hint: Use {w: 1} to wait for one server acknowledgment [OK]
Common Mistakes:
  • Using string 'true' instead of number 1
  • Confusing 'majority' with numeric 1
  • Setting w to 0 which means no acknowledgment
3.

What will happen if you run this MongoDB command?

db.orders.insertOne({item: 'book'}, {writeConcern: {w: 0}})
medium
A. The insert does not wait for any confirmation
B. The insert waits for confirmation from the server
C. The insert waits for confirmation from majority of servers
D. The insert throws a syntax error

Solution

  1. Step 1: Understand writeConcern w: 0 meaning

    Setting w: 0 means no acknowledgment is required from the server.
  2. Step 2: Predict behavior of insertOne with w: 0

    The insert operation will send data but not wait for any confirmation, so it returns immediately.
  3. Final Answer:

    The insert does not wait for any confirmation -> Option A
  4. Quick Check:

    w: 0 means no wait for confirmation [OK]
Hint: w: 0 means fire-and-forget, no wait [OK]
Common Mistakes:
  • Thinking w: 0 waits for server confirmation
  • Assuming syntax error due to w: 0
  • Confusing w: 0 with majority write concern
4.

Identify the error in this MongoDB write concern usage:

db.users.insertOne({name: 'Bob'}, {writeConcern: {w: 'two'}})
medium
A. The write concern should be set as a string without quotes
B. The value 'two' is invalid for write concern w
C. The insertOne method does not accept write concern
D. The document format is incorrect

Solution

  1. Step 1: Check valid values for write concern w

    Write concern w accepts numbers or 'majority', not arbitrary strings like 'two'.
  2. Step 2: Identify the error in the given code

    Using 'two' is invalid and will cause an error.
  3. Final Answer:

    The value 'two' is invalid for write concern w -> Option B
  4. Quick Check:

    Write concern w must be number or 'majority' [OK]
Hint: Write concern w must be number or 'majority' only [OK]
Common Mistakes:
  • Using invalid string values for w
  • Thinking writeConcern is not allowed in insertOne
  • Confusing quotes usage in write concern
5.

You want to ensure your MongoDB write operation waits for confirmation from the majority of replica set members but also times out if it takes more than 5 seconds. Which write concern option should you use?

hard
A. {w: 0, wtimeout: 5000}
B. {w: 1, wtimeout: 5000}
C. {w: 'majority', wtimeout: 5000}
D. {w: 'majority', wtimeout: 0}

Solution

  1. Step 1: Understand write concern for majority

    To wait for majority confirmation, w must be set to 'majority'.
  2. Step 2: Add timeout for waiting

    Use wtimeout to specify max wait time in milliseconds; 5000 means 5 seconds.
  3. Step 3: Combine options correctly

    The correct option is {w: 'majority', wtimeout: 5000} to wait for majority with 5 seconds timeout.
  4. Final Answer:

    {w: 'majority', wtimeout: 5000} -> Option C
  5. Quick Check:

    Majority + 5s timeout = {w: 'majority', wtimeout: 5000} [OK]
Hint: Use w: 'majority' with wtimeout in ms for timeout [OK]
Common Mistakes:
  • Using w: 1 instead of 'majority' for majority confirmation
  • Setting wtimeout to 0 which means no timeout
  • Using w: 0 which disables waiting