OPTIONS

Write Concern

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.

See also

Write Concern Reference for a reference of specific write concern configuration. Also consider Write Operations for a general overview of write operations with MongoDB and Write Concern for Replica Sets for considerations specific to replica sets.

Note

The driver write concern change created a new connection class in all of the MongoDB drivers. The new class, called MongoClient, changed the default write concern. See the release notes for this change and the release notes for your driver.

Write Concern Levels

Clients issue write operations with some level of write concern. 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 operation. Unacknowledged is similar to errors ignored; however, drivers attempt to receive and handle network errors when possible. The driver’s ability to detect network errors depends on the system’s networking configuration.

To set unacknowledged write concern, specify w values of 0 to your driver.

Before the releases outlined in Default Write Concern Change, this was the default write concern.

Write operation to a ``mongod`` instance with write concern of ``unacknowledged``. The client does not wait for any acknowledgment.

Acknowledged

With a receipt acknowledged write concern, the mongod confirms the receipt of the write operation. Acknowledged write concern allows clients to catch network, duplicate key, and other errors.

To set acknowledged write concern, specify w values of 1 to your driver.

MongoDB uses acknowledged write concern by default, after the releases outlined in Default Write Concern Change.

Write operation to a ``mongod`` instance with write concern of ``acknowledged``. The client waits for acknowledgment of success or exception.

Internally, the default write concern calls getLastError with no arguments. For replica sets, you can define the default write concern settings in the getLastErrorDefaults. When getLastErrorDefaults does not define a default write concern setting, getLastError defaults to basic receipt acknowledgment.

Journaled

With a journaled write concern, the mongod 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.

To set a journaled write concern, specify w values of 1 and set the journal or j option to true for your driver. 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, you can increase the frequency that MongoDB commits operations to the journal. See journalCommitInterval for more information.

Write operation to a ``mongod`` instance with write concern of ``journaled``. The ``mongod`` sends acknowledgment after it commits the write operation to the journal.

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 add several considerations for write concern. Basic write concerns affect write operations on only one mongod instance. The w argument to getLastError provides replica acknowledged write concerns. With replica acknowledged you can guarantee that the write operation propagates to the members of a replica set. See Write Concern Reference document for the values for w and Write Concern for Replica Sets for more information.

To set replica acknowledged write concern, specify w values greater than 1 to your driver.

Write operation to a replica set with write concern level of ``w:2`` or write to the primary and at least one secondary.

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.