What are read preference and write concern in MongoDB?

IntermediateConceptMongoDB
Answer

Short answer

Write concern controls how many members must acknowledge a write before it's considered successful (w: 1, w: "majority"). Read preference controls which members reads go to (primary, primaryPreferred, secondary, nearest). Together they trade off consistency, durability, latency and load.

Write concern

Setting Meaning
w: 1 Primary acknowledged — fast, could be lost if primary fails before replicating
w: "majority" Most members have it — survives failover (default in modern MongoDB)
j: true Written to the on-disk journal

Read preference

Setting Reads from Note
primary (default) Primary only Always latest data
primaryPreferred Primary, secondary if primary is down
secondary Secondaries Offloads reads, may be stale
nearest Lowest latency member Multi-region reads

Connection string example

mongodb://.../shop?replicaSet=rs0&w=majority&readPreference=secondaryPreferred

What to say in the interview

"Write concern sets durability — majority means a write survives failover. Read preference sets where reads go — primary for consistency, secondaries for scaling reads with possible staleness. For orders we use w majority and primary reads; for analytics, secondary reads."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MongoDB basics

More on MongoDB