Write concern sets the acknowledgment a MongoDB write must receive before it is reported as successful. In a replica set, requiring more members to acknowledge a write improves durability but can increase response time while MongoDB waits for confirmation. The default is w: "majority". section 9.5.1
Choose the w value to match the cost of losing a write:
- w: 0 requires no acknowledgment. It favors speed when losing some entries, such as logs, is acceptable. section 9.5.1
- w: 1 requires acknowledgment only from the primary. It responds quickly, but a write may be lost if the primary fails before replication. section 9.5.1
- w: "majority" requires acknowledgment from a majority of voting members. It suits writes such as orders that must remain recoverable after a primary failure. section 9.5.1
- w: "all" requires every replica set member to acknowledge the write. It offers the strongest acknowledgment requirement here, potentially with a substantial delay. section 9.5.1
db.routes.insertOne(
{ src_airport: 'MUC'},
{ writeConcern: { w: "majority", j: true, wtimeout: 5000 } }
)
This insert supplies write concern on the operation: w: "majority", alongside the j and wtimeout options. Write-concern settings use the form { w: <value>, j: <boolean>, wtimeout: <number> }. section 9.5.1
Write concern works alongside read concern and read preference. For an application that reads its own writes from the primary, pair w: "majority" with "majority" read concern. For multidocument transactions, configure appropriate read and write concerns as part of the reliability plan, along with error handling and retries for transient failures. section 9.5 section 8.5
Table 10.7 Atlas custom Write Concerns for multiregion clusters
| Write concern |
Tags |
Description |
|
|
|
Write operations must receive acknowledgment from at least two regions in your cluster. |
|
|
|
Write operations must receive acknowledgment from at least three regions in your cluster. |
|
|
|
Write operations must be acknowledged by at least two different regions in your cluster, each hosted by a distinct cloud provider. |
For Atlas multiregion clusters, custom write concerns can require acknowledgment across two or three regions, or across two regions hosted by distinct cloud providers. section 10.6
db.routes.insertOne(
{
airline: { id: 410, name: 'Lufthansa', alias: 'LH', iata: 'DLH' },
src_airport: 'MUC',
dst_airport: 'JFK'
},
{ writeConcern: { w: "threeRegions" } }
)
To require propagation to three regions, place w: "threeRegions" in the insert operation’s writeConcern document. section 10.6
