@@ -21,60 +21,77 @@ Write Concern
21
21
22
22
When a :term:`client` sends a write operation to a database server,
23
23
the operation will return without waiting for the operation to succeed
24
- or complete. To verify that the operation has completed,
25
- and to check for an error, use the
26
- :dbcommand:`getLastError` command. You can configure
27
- :dbcommand:`getLastError` to wait until the journal flushes to disk or the data itself flushes to disk. For replica sets, you can
28
- configure :dbcommand:`getLastError` to return only after the write
29
- operation has propagated to more than one member of the set or to a
30
- majority of the set's members.
24
+ or complete. The :dbcommand:`getLastError` command provides a way to
25
+ check if write operations have succeeded, and provides operations to
26
+ require various levels of write concern. [#write-concern]_ These
27
+ levels are:
31
28
32
- Many drivers have a "safe" mode or "write concern" that automatically
33
- issues a :dbcommand:`getLastError` command following write
34
- operations to ensure that they complete. "Safe mode,"
35
- provides confirmation of write operations to the client.
29
+ - without options. Confirms that the server has received the write
30
+ operations, and modified the :program:`mongod` instances in-memory
31
+ representation of the data.
36
32
37
- However, safe writes can take longer to return and are not required in
38
- all applications. Using the ``w: "majority"`` option for
39
- :dbcommand:`getLastError`, write operations to a replica set will
40
- return only after a write operation has replicated to a majority of
41
- the members of the set. At the :program:`mongo` shell, use the
42
- following command to ensure that writes have propagated to a majority
43
- of the replica set members:
33
+ - with the ``journal`` option. Confirms that the server has committed
34
+ the data to the on-disk journal. This ensures that the data will be
35
+ durable in the event that :program:`mongod` experiences an
36
+ unexpected shut down.
37
+
38
+ - with the ``fsync`` option. Confirms that the :program:`mongod` has
39
+ flushed the write operation to disk.
40
+
41
+ - for replica sets, with the ``w`` option. Confirms that the write
42
+ operation has replicated to the specified number of
43
+ replica set members. You may specify a number of servers *or*
44
+ ``majority`` to ensure that the write propagates to a majority of
45
+ set members.
46
+
47
+ Many drivers have a "safe" mode or "write concern" that automatically
48
+ issues a :dbcommand:`getLastError` command following write operations
49
+ to ensure that they complete. "Safe mode," provides confirmation of
50
+ write operations to the client. However, safe writes can take longer
51
+ to return and are not required in all applications. Consider the
52
+ following operations.
44
53
45
54
.. code-block:: javascript
46
55
47
56
db.runCommand( { getLastError: 1, w: "majority" } )
48
57
db.getLastErrorObj("majority")
49
58
50
- You may also specify ``w: 2`` so that the write operation replicates
51
- to a second member before the command returns.
59
+ Following a write operation, these equivalent
60
+ :dbcommand:`getLastError` operations will ensure that write operations
61
+ return only after a write operation has replicated to a majority of
62
+ the members of the set. You might also specify ``w: 2`` so that the
63
+ write operation replicates to a second member before the command
64
+ returns.
52
65
53
66
.. note::
54
67
55
- :dbcommand:`getLastError` assumes the primary replica node,
56
- therefore, ``w: 2`` waits until the :term:`primary` and one other
57
- member of the replica set acknowledges the write operation. The current
58
- primary always counts as ``w: 1``. Using ``w: 0`` makes no sense and should
59
- not be done.
68
+ :dbcommand:`getLastError` assumes the primary replica node, which
69
+ is equivalent to ``w: 1``. ``w: 2`` waits until 2 members of the
70
+ set, or the :term:`primary` and one other member of the replica set
71
+ before confirming the write operation.
60
72
61
- You can also configure a "default" :dbcommand:`getLastError` behavior on the
62
- replica set configuration. For instance:
73
+ Using ``w: 0`` is valid but does not produce useful behavior.
74
+
75
+ You can also configure a "default" :dbcommand:`getLastError` behavior
76
+ for the replica set configuration. Use the
77
+ :data:`settings.getLastErrorDefaults` setting in the :doc:`replica set
78
+ configuration </reference/replica-configuration>`. For instance:
63
79
64
80
.. code-block:: javascript
65
81
66
82
cfg = rs.conf()
67
83
cfg.settings.getLastErrorDefaults = {w: "majority", j: true}
68
84
rs.reconfig(cfg)
69
85
70
- When the new configuration is active, the effect of the
71
- :dbcommand:`getLastError` operation will wait until the write
72
- operation has completed on a majority of the set members before writing. By
73
- specifying ``j: true`` a complete commit of the operations to the
74
- journal is all that :dbcommand:`getLastError` requires to return. Use the
75
- :data:`getLastErrorDefaults`" setting in the :ref:`replica set` configuration
76
- to define the standards for a set-wide "safe mode."
77
- The default setting will only affect :dbcommand:`getLastError`
86
+ When the new configuration is active, the :dbcommand:`getLastError`
87
+ operation will wait for the write operation to complete on a majority
88
+ of the set members before returning. By specifying ``j: true``,
89
+ :dbcommand:`getLastError` waits for a complete commit of the
90
+ operations to the journal before returning.
91
+
92
+ Use the :data:`getLastErrorDefaults`" setting in the :ref:`replica
93
+ set` configuration to define the standards for a set-wide "safe mode."
94
+ The default setting will only affect :dbcommand:`getLastError`
78
95
commands with *no* other arguments.
79
96
80
97
.. index:: read preference
@@ -516,7 +533,7 @@ Database Commands
516
533
517
534
Because some :term:`database commands <database command>` read and
518
535
return data from the database, all of the official drivers support
519
- full :ref:`read preference mode semantics <replica-set-read-preference-modes>`
536
+ full :ref:`read preference mode semantics <replica-set-read-preference-modes>`
520
537
for the following commands:
521
538
522
539
- :dbcommand:`group`
0 commit comments