@@ -45,26 +45,23 @@ documentation for your client library.
45
45
Write Concern
46
46
-------------
47
47
48
- .. DONE The information on the replica-set page has now been split between
49
- this page and the replica-set page.
50
-
51
48
:term:`Write concern <write concern>` confirms the success of write
52
49
operations to MongoDB databases by returning an object indicating
53
50
operational success. Beginning with version 2.2.x, the :program:`mongo`
54
- shell enables write concern by default. In previous versions the shell
55
- disabled write concern by default.
51
+ shell and MongoDB drivers enable write concern by default. Prior to
52
+ 2.2.x, the shell disables write concern by default, while the behavior
53
+ for drivers varies. For your driver's behavior, see the
54
+ :doc:`/applications/drivers` documentation.
56
55
57
- Many drivers also enable write concern by default. For information on
58
- your driver, see the :doc:`/applications/drivers` documentation.
59
56
60
57
.. todo add note about all drivers after `date` will have w:1 write
61
58
concern for all operations by default.
62
59
63
60
Write concern issues the :dbcommand:`getLastError` command after write
64
- operations, and the command returns an object with success information.
65
- The returned object's ``err`` field contains either a value of ``null``
66
- to indicate no errors, in which case the write operations have completed
67
- successfully, or contains a description of the last error encountered.
61
+ operations to return an object with success information. The returned
62
+ object's ``err`` field contains either a value of ``null``, which
63
+ indicates write operations have completed successfully, or contains a
64
+ description of the last error encountered.
68
65
69
66
A successful write operation means the :program:`mongod` instance
70
67
received the write operation and has committed the operation to the
@@ -74,81 +71,104 @@ detect situations where the :program:`mongod` instance becomes
74
71
inaccessible or detect insertion errors caused by :ref:`duplicate key errors
75
72
<index-type-unique>`.
76
73
74
+ Write concern provides confirmation of write operations but also adds to
75
+ performance costs. In situations where confirmation is unnecessary, it
76
+ can be advantageous to disable write concern.
77
+
77
78
You can modify the level of write concern returned by issuing the
78
79
:dbcommand:`getLastError` command with one or both of following options:
79
80
80
81
- ``j`` or "journal" option
81
82
82
- In addition to the default confirmation, this option confirms that the
83
- :program:`mongod` instance has written the data to the on-disk
84
- journal. This ensures the data is durable if :program:`mongod` or the
85
- server itself crashes or shuts down unexpectedly.
83
+ This option confirms that the :program:`mongod` instance has written
84
+ the data to the on-disk journal and ensures data is not lost if the
85
+ :program:`mongod` instance shuts down unexpectedly. Set to ``true`` to
86
+ enable, as shown in the following example:
87
+
88
+ .. code-block:: javascript
89
+
90
+ db.runCommand( { getLastError: 1, j: "true" } )
86
91
87
92
- ``w`` option
88
93
89
94
This option is used either to configure write concern on members of
90
- :term:`replica sets <replica set>` or to disable write concern. By
91
- default, the ``w`` option is set to ``1``, which enables write
92
- concern on a single :program:`mongod` instance.
95
+ :term:`replica sets <replica set>` *or* to disable write concern
96
+ entirely. By default, the ``w`` option is set to ``1``, which enables
97
+ write concern on a single :program:`mongod` instance or on the
98
+ :term:`primary` in a replica set.
93
99
94
- In the case of replica sets, the value of ``1`` enables write concern
95
- on the :term:`primary` only. To configure write concern to confirm
96
- that writes have replicated to a specified number of replica set
97
- members, see :ref:`Write Concern for Replica Sets
98
- <replica-set-write-concern>`.
100
+ The ``w`` option takes the following values:
99
101
100
- To disable write concern, set the ``w`` option to ``0``, as shown in
101
- the following example:
102
+ - ``-1``
102
103
103
- .. code-block:: javascript
104
+ Turns off reporting of network errors.
104
105
105
- db.runCommand( { getLastError: 1, w: 0 } )
106
+ - ``0``
106
107
107
- .. note:: Write concern provides confirmation of write operations but also adds
108
- to performance costs. In situations where confirmation is
109
- unnecessary, it can be advantageous to disable write concern.
108
+ Disables write concern.
110
109
111
- .. _write-operations-bulk-insert:
110
+ .. note:: If you disable write concern but enable the journal
111
+ option, as shown here:
112
112
113
- Bulk Inserts
114
- ------------
113
+ .. code-block:: javascript
115
114
116
- Bulk inserts let you insert many documents in a single database call.
115
+ { getLastError: 1, w: 0, j: "true" }
117
116
118
- Bulk inserts allow MongoDB to distribute the performance penalty when
119
- performing inserts to a large number of documents at once. Bulk inserts
120
- let you pass multiple events to the :method:`insert()` method at once.
121
- All write concern options apply to bulk inserts.
117
+ The setting with the ``j`` option prevails. Write concern is
118
+ enabled with journaling.
122
119
123
- You perform bulk inserts through your driver. See the
124
- :doc:`/applications/drivers` documentation for your driver for how to do
125
- bulk inserts.
120
+ - ``1``
126
121
127
- Beginning with version 2.2, you also can perform bulk inserts through
128
- the :program:`mongo` shell .
122
+ Enables write concern on a standalone :program:`mongod` or the
123
+ :term:`primary` in a replica set .
129
124
130
- Beginning with version 2.0, you can set the ``ContinueOnError`` flag for
131
- bulk inserts to signal inserts should continue even if one or more from
132
- the batch fails. In that case, if multiple errors occur, only the most
133
- recent is reported by the :dbcommand:`getLastError` command. For a
134
- :term:`sharded collection`, ``ContinueOnError`` is implied and cannot be
135
- disabled.
125
+ - *A number greater than 1*
126
+
127
+ Confirms that write operations have replicated to the specified
128
+ number of replica set members, including the primary. If you set
129
+ ``w`` to a number that is greater than the number of set members
130
+ that hold data, MongoDB waits for the non-existent members become
131
+ available, which means MongoDB blocks indefinitely.
132
+
133
+ - ``majority``
134
+
135
+ Confirms that write operations have replicated to the majority of
136
+ set members.
137
+
138
+ For more information on write concern and replica sets, see :ref:`Write
139
+ Concern for Replica Sets <replica-set-write-concern>`.
140
+
141
+ .. _write-operations-bulk-insert:
142
+
143
+ Bulk Inserts
144
+ ------------
136
145
137
- If you insert data without write concern, the bulk insert gain might be
138
- insignificant. But if you insert data with write concern configured,
139
- bulk insert can bring significant performance gains by distributing the
140
- penalty over the group of inserts.
146
+ Bulk inserts let you insert many documents at once in a single database
147
+ call by letting you pass multiple documents to a single insert
148
+ operation.
141
149
142
- MongoDB is quite fast at a series of singleton inserts. Thus one often
143
- does not need to use this specialized version of insert.
150
+ Bulk insert can significantly increase performance by distributing
151
+ :ref:`write concern <write-operations-write-concern>` costs. Beginning
152
+ in version 2.2.x, write concern is enabled by default.
144
153
145
154
Bulk inserts are often used with :term:`sharded collections <sharded
146
155
collection>` and are more effective when the collection is already
147
156
populated and MongoDB has already determined the key distribution. For
148
157
more information on bulk inserts into sharded collections, see
149
158
:ref:`sharding-bulk-inserts`.
150
159
151
- If possible, consider using bulk inserts to insert event data.
160
+ When performing bulk inserts through a driver, you can use the
161
+ ``ContinueOnError`` option in your driver's insert command to continue
162
+ to insert remaining documents even if an insert fails. This option is
163
+ available in MongoDB versions 2.0 and higher. If errors occur, only the
164
+ most recent is reported. For a :term:`sharded collection`,
165
+ ``ContinueOnError`` is implied and cannot be disabled. For details on
166
+ performing bulk inserts through your driver, see the
167
+ :doc:`/applications/drivers` documentation for your driver.
168
+
169
+ Beginning with version 2.2, you can perform bulk inserts through the
170
+ :program:`mongo` shell by passing an array of documents to the
171
+ :method:`insert() <db.collection.insert()>` method.
152
172
153
173
For more information, see :ref:`write-operations-sharded-clusters`,
154
174
:ref:`sharding-bulk-inserts`, and :doc:`/administration/import-export`.
0 commit comments