- MongoDB CRUD Operations >
- MongoDB CRUD Concepts >
- Write Operations >
- Write Concern
Write Concern¶
On this page
Write concern describes the guarantee that MongoDB provides when reporting on the success of a write operation. The strength of the write concerns determine the level of guarantee. When inserts, updates and deletes have a weak write concern, write operations return quickly. In some failure cases, write operations issued with weak write concerns may not persist. With stronger write concerns, clients wait after sending a write operation for MongoDB to confirm the write operations.
MongoDB provides different levels of write concern to better address the specific needs of applications. Clients may adjust write concern to ensure that the most important operations persist successfully to an entire MongoDB deployment. For other less critical operations, clients can adjust the write concern to ensure faster performance rather than ensure persistence to the entire deployment.
Changed in version 2.6: A new protocol for write operations integrates write concern with the write operations.
For details on write concern configurations, see Write Concern Reference.
Considerations¶
Default Write Concern¶
The mongo
shell and the MongoDB drivers use
Acknowledged as the default write concern.
See Acknowledged for more information, including when this write concern became the default.
Timeouts¶
Clients can set a wtimeout value as part of a replica acknowledged write concern. If the write concern is not satisfied in the specified interval, the operation returns an error, even if the write concern will eventually succeed.
MongoDB does not “rollback” or undo modifications made before the
wtimeout
interval expired.
Write Concern Levels¶
MongoDB has the following levels of conceptual write concern, listed from weakest to strongest:
Unacknowledged¶
With an unacknowledged write concern, MongoDB does not acknowledge the receipt of write operations. Unacknowledged is similar to errors ignored; however, drivers will attempt to receive and handle network errors when possible. The driver’s ability to detect network errors depends on the system’s networking configuration.
Before the releases outlined in Default Write Concern Change, this was the default write concern.
Acknowledged¶
With a receipt acknowledged write concern, the mongod
confirms that it received the write operation and applied the change
to the in-memory view of data. Acknowledged write concern allows
clients to catch network, duplicate key, and other errors.
MongoDB uses the acknowledged write concern by default starting in the driver releases outlined in Releases.
Changed in version 2.6: The mongo
shell write methods now incorporates the
write concern in the write methods and
provide the default write concern whether run interactively or in a
script. See Write Method Acknowledgements for details.
Acknowledged write concern does not confirm that the write operation has persisted to the disk system.
Journaled¶
With a journaled write concern, the MongoDB acknowledges the write operation only after committing the data to the journal. This write concern ensures that MongoDB can recover the data following a shutdown or power interruption.
You must have journaling enabled to use this write concern.
With a journaled write concern, write operations must wait for the next
journal commit. To reduce latency for these operations, MongoDB
also increases the frequency that it commits operations to the journal. See
commitIntervalMs
for more information.
Note
Requiring journaled write concern in a replica set only requires a journal commit of the write operation to the primary of the set regardless of the level of replica acknowledged write concern.
Replica Acknowledged¶
Replica sets present additional considerations with regards to write concern. The default write concern only requires acknowledgement from the primary.
With replica acknowledged write concern, you can guarantee that the write operation propagates to additional members of the replica set. See Write Concern for Replica Sets for more information.
Note
Requiring journaled write concern in a replica set only requires a journal commit of the write operation to the primary of the set regardless of the level of replica acknowledged write concern.
See also