@@ -6,15 +6,14 @@ Write Operations
6
6
7
7
All operations that create or modify data in the MongoDB instance are
8
8
write operations. MongoDB represents data as :term:`BSON documents
9
- <document>` stored in a collection :term:`collections
10
- <collection>`. Write operations target a single collection and are
11
- atomic on the level of a single document: no single write operation
12
- can atomically affect more than one document or more than one
13
- collection.
9
+ <document>` stored in :term:`collections <collection>`. Write
10
+ operations target a single collection and are atomic on the level of a
11
+ single document: no single write operation can atomically affect more
12
+ than one document or more than one collection.
14
13
15
- This document describes the syntax of all write operations in MongoDB,
16
- and describes their operation, and strategies increasing the
17
- efficiency of writes in applications.
14
+ This document introduces the write operators available in MongoDB as
15
+ well as presents strategies to increase the efficiency of writes in
16
+ applications.
18
17
19
18
.. index:: write operators
20
19
.. _write-operations-operators:
@@ -29,8 +28,8 @@ database, see the following pages:
29
28
- :doc:`/applications/update`
30
29
- :doc:`/applications/delete`
31
30
32
- For information on specific methods in the :program:`mongo` shell used
33
- for write operations , see the following:
31
+ For information on specific methods used to perform write operations in the
32
+ :program:`mongo` shell , see the following:
34
33
35
34
- :method:`db.collection.insert()`
36
35
- :method:`db.collection.update()`
@@ -54,43 +53,42 @@ Write Concern
54
53
Operational Considerations and Write Concern
55
54
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
55
57
- Clients issue all write operations with some level of :term:`Write
58
- concern <write concern> `, which describes level of concern or the kind
59
- of guarantee the server will provide in its response to a write
60
- operation. Consider the following levels of conceptual write concern:
56
+ Clients issue write operations with some level of :term:`write
57
+ concern`, which describes the level of concern or the kind of
58
+ guarantee the server will provide in its response to a write operation.
59
+ Consider the following levels of conceptual write concern:
61
60
62
61
- *none*: Write operations are not acknowledged, and may be lost if
63
62
there is a connection error that the client is not yet aware of, or
64
63
if the :program:`mongod` produces an exception (e.g. a duplicate key
65
64
exception for :ref:`unique indexes <index-type-unique>`.) While this
66
65
operation is efficient because it does not require the database to
67
66
respond to every write operation, it also incurs a significant risk
68
- about the persistence and durability of the data.
67
+ in regards to the persistence and durability of the data.
69
68
70
69
Before the releases outlined in :ref:`driver-write-concern-change`,
71
70
this was the default write concern.
72
71
73
- - *receipt acknowledgment*: The :program:`mongod` will confirm
72
+ - *receipt acknowledgment*: The :program:`mongod` will confirm the
74
73
receipt of the write operation, allowing the client to catch
75
74
network, duplicate key, and other exceptions. After the releases
76
75
outlined in :ref:`driver-write-concern-change`, this is the default
77
76
write concern. [#default-write-concern]_
78
77
79
78
- *journal commit*: The :program:`mongod` will confirm the write
80
- operation only after it has written operation to the
81
- :term:`journal`. This confirms that the write operation can survive
82
- a :program:`mongod` shutdown and ensures that the write operation is
83
- durable.
84
-
85
- While receipt acknowledgment provides the fundamental basis
86
- for write concern, without requiring a journal commit in the write
87
- concern, there is an up-to 100 millisecond window between journal
88
- commits where the write operation is not fully durable. Require
89
- journal commit as part of the write concern to provide this
90
- durability guarantee.
91
-
92
- :term:`Replica sets <replica set>` add an additional layer of
93
- consideration for write concern. Basic write concerns level affect the
79
+ operation only after it has written the operation to the
80
+ :term:`journal`. This confirms that the write operation can survive a
81
+ :program:`mongod` shutdown and ensures that the write operation is
82
+ durable.
83
+
84
+ While *receipt acknowledgment* without *journal commit* provides the
85
+ fundamental basis for write concern, there is an up-to 100
86
+ millisecond window between journal commits where the write operation
87
+ is not fully durable. Require *journal commit* as part of the write
88
+ concern to provide this durability guarantee.
89
+
90
+ :term:`Replica sets <replica set>` present an additional layer of
91
+ consideration for write concern. Basic write concern level affect the
94
92
write operation on only one :program:`mongod` instance. The ``w``
95
93
argument to :dbcommand:`getLastError` makes it possible to guarantee
96
94
that the write operation has propagated to the members of a replica
@@ -99,13 +97,12 @@ set. See the :ref:`Write Concern for Replica Sets
99
97
100
98
101
99
.. [#default-write-concern] The default write concern is to call
102
- :dbcommand:`getLastError` with no arguments. For replica sets with
103
- :data:`getLastErrorDefaults <settings.getLastErrorDefaults>`
104
- settings, the default write concern will be whatever
105
- :data:`getLastErrorDefaults <settings.getLastErrorDefaults>`
106
- defines. If :data:`getLastErrorDefaults <settings.getLastErrorDefaults>`
107
- defines no default write concern, :dbcommand:`getLastError` will
108
- provide basic receipt acknowledgment.
100
+ :dbcommand:`getLastError` with no arguments. For replica sets, you
101
+ can define the default write concern settings in the
102
+ :data:`getLastErrorDefaults <settings.getLastErrorDefaults>`. If
103
+ :data:`getLastErrorDefaults <settings.getLastErrorDefaults>` does
104
+ not define a default write concern setting,
105
+ :dbcommand:`getLastError` defaults to basic receipt acknowledgment.
109
106
110
107
.. _write-concern-operation:
111
108
.. _write-concern-internals:
@@ -114,23 +111,24 @@ Internal Operation of Write Concern
114
111
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115
112
116
113
To provide write concern, :doc:`drivers </applications/drivers>` issue
117
- the :dbcommand:`getLastError` command after a write opeation and
118
- receive a document with information about the last operation. The this
114
+ the :dbcommand:`getLastError` command after a write operation and
115
+ receive a document with information about the last operation. This
119
116
document's ``err`` field contains either:
120
117
121
- - ``null``, which indicates write operations have completed
118
+ - ``null``, which indicates the write operations have completed
122
119
successfully, or
123
120
124
121
- a description of the last error encountered.
125
122
126
123
The definition of a "successful write" depends on the arguments
127
- specified to :dbcommand:`getLastError` or in replica sets the
128
- configuration of :data:`getLastErrorDefaults <settings.getLastErrorDefaults>`.
129
- When deciding on what level of write concern your application needs,
130
- become familiar with the :ref:`write-concern-considerations`.
124
+ specified to :dbcommand:`getLastError`, or in replica sets, the
125
+ configuration of :data:`getLastErrorDefaults
126
+ <settings.getLastErrorDefaults>`. When deciding the level of write
127
+ concern for your application, become familiar with the
128
+ :ref:`write-concern-considerations`.
131
129
132
- The :dbcommand:`getLastError` has the following options, that you can
133
- use to define write concern requirements.
130
+ The :dbcommand:`getLastError` has the following options to configure write
131
+ concern requirements.
134
132
135
133
- ``j`` or "journal" option
136
134
@@ -145,9 +143,9 @@ use to define write concern requirements.
145
143
146
144
- ``w`` option
147
145
148
- This option provides write concern operations for :term:`replica
149
- sets <replica set>` *and* is also used to disable write concern
150
- entirely . By default, the ``w`` option is set to ``1``, which
146
+ This option provides the ability to disable write concern entirely *as
147
+ well as* specifies the write concern operations for :term:`replica sets
148
+ <replica set>` . By default, the ``w`` option is set to ``1``, which
151
149
provides basic receipt acknowledgment on a single :program:`mongod`
152
150
instance or on the :term:`primary` in a replica set.
153
151
@@ -160,12 +158,13 @@ use to define write concern requirements.
160
158
161
159
- ``0``:
162
160
163
- Disables basic acknowledgment of write operations, but does
164
- pass information about networking errors to the application.
161
+ Disables basic acknowledgment of write operations, but returns
162
+ information about networking errors to the application.
165
163
166
164
.. note:: If you disable basic operation acknowledgment write
167
- concern but enable the journal commit write concern, as
168
- in the following operation:
165
+ concern but enable the journal commit write concern, the journal
166
+ commit overrides the no write concern, as in the following
167
+ operation:
169
168
170
169
.. code-block:: javascript
171
170
@@ -182,29 +181,27 @@ use to define write concern requirements.
182
181
183
182
- *A number greater than 1*:
184
183
185
- Guarantees that write write operations have propagated
186
- successfully to the specified number of replica set members
187
- including the primary. If you set ``w`` to a number that is
188
- greater than the number of set members that hold data, MongoDB
189
- waits for the non-existent members become available, which means
190
- MongoDB blocks indefinitely.
184
+ Guarantees that write operations have propagated successfully to
185
+ the specified number of replica set members including the primary.
186
+ If you set ``w`` to a number that is greater than the number of set
187
+ members that hold data, MongoDB waits for the non-existent members
188
+ to become available, which means MongoDB blocks indefinitely.
191
189
192
190
- ``majority``:
193
191
194
192
Confirms that write operations have propagated to the majority of
195
- configured replica set nodes must acknowledge the write operation
193
+ configured replica set; nodes must acknowledge the write operation
196
194
before it succeeds. This ensures that write operation will *never*
197
195
be subject to a rollback in the course of normal operation, and
198
- furthemore allows you to prevent hard coding assumptions about the
196
+ furthermore allows you to prevent hard coding assumptions about the
199
197
size of your replica set into your application.
200
198
201
199
- *A tag set*:
202
200
203
- By specifying a :ref:`tag set
204
- <replica-set-configuration-tag-sets>` you can have much more fine
205
- grained control over which members of the replica set must
206
- acknowledge a write operation to satisfy the required level of
207
- write concern.
201
+ By specifying a :ref:`tag set <replica-set-configuration-tag-sets>`
202
+ you can have fine-grained control over which replica
203
+ set members must acknowledge a write operation to satisfy the required
204
+ level of write concern.
208
205
209
206
For more information on write concern and replica sets, see :ref:`Write
210
207
Concern for Replica Sets <replica-set-write-concern>` for more information..
@@ -218,13 +215,13 @@ Bulk Inserts
218
215
------------
219
216
220
217
In some situations you may need to insert or ingest a large amount of
221
- data into a MongoDB database. These *bulk inserts*, have some
218
+ data into a MongoDB database. These *bulk inserts* have some
222
219
special considerations that are different from other write
223
220
operations.
224
221
225
222
The :method:`insert() <db.collection.insert()>` method, when passed an
226
- array of documents will perform a " bulk insert," and insert each
227
- document separately . :doc:`Drivers </applications/drivers>` each
223
+ array of documents, will perform a bulk insert, and inserts each
224
+ document atomically . :doc:`Drivers </applications/drivers>`
228
225
provide their own interface for this kind of operation.
229
226
230
227
.. versionadded:: 2.2
@@ -270,10 +267,10 @@ After every insert, update, or delete operation, MongoDB must update
270
267
itself. Therefore, every index on a collection adds some amount of
271
268
overhead for the performance of write operations.
272
269
273
- In general, the performance gains that indexes realize for *read
270
+ In general, the performance gains that indexes provide for *read
274
271
operations* are worth the insertion penalty; however, when optimizing
275
- write performance be careful when creating new indexes and always
276
- evaluate the indexes on the collection and ensure that your quires are
272
+ write performance, be careful when creating new indexes and always
273
+ evaluate the indexes on the collection and ensure that your queries are
277
274
actually using these indexes.
278
275
279
276
For more information on indexes in MongoDB consider :doc:`/indexes`
@@ -309,37 +306,35 @@ In :term:`replica sets <replica set>`, all write operations go to the
309
306
set's :term:`primary`, which applies the write operation then records
310
307
the operations on the primary's operation log or :term:`oplog`. The
311
308
oplog is a reproducible sequence of operations to the data
312
- set. :term:`Secondary` members of the set are always replicating the
309
+ set. :term:`Secondary` members of the set are continuously replicating the
313
310
oplog and applying the operations to themselves in an asynchronous
314
311
process.
315
312
316
- Large volumes of write operations, particularly bulk operations may
317
- create situations where secondary members have a difficult time
318
- applying replicating operations from the primary fast enough, which
313
+ Large volumes of write operations, particularly bulk operations, may
314
+ create situations where the secondary members have difficulty applying
315
+ the replicating operations from the primary at a fast enough rate; this
319
316
can cause their state to fall behind that of the primary. Secondaries
320
317
that are significantly behind the primary present problems for normal
321
318
operation of the replica set, particularly :ref:`failover
322
319
<replica-set-failover-administration>` in the form of :ref:`rollbacks
323
- <replica-set-rollback>` as well as general
324
- :ref:`read consistency <replica-set-consistency>`.
325
-
326
- To help avoid this issue, you can customize :ref:`write concern
327
- <write-operations-write-concern>` to return confirm confirmation of
328
- the write operation to another member [#write-concern-throttling]_ of
329
- the replica set ``100`` or ``1,000``) operations. This provides an
330
- opportunity for secondaries to catch up with the primary. Write
331
- concern can slow the overall progress of write operations but ensure
332
- that the secondaries can maintain a largely current state with respect
333
- to the primary.
320
+ <replica-set-rollback>` as well as general :ref:`read consistency
321
+ <replica-set-consistency>`.
322
+
323
+ To help avoid this issue, you can customize the :ref:`write concern
324
+ <write-operations-write-concern>` to return confirmation of the write
325
+ operation to another member [#write-concern-throttling]_ of the replica
326
+ set every 100 or 1,000 operations. This provides an opportunity for
327
+ secondaries to catch up with the primary. Write concern can slow the
328
+ overall progress of write operations but ensure that the secondaries
329
+ can maintain a largely current state with respect to the primary.
334
330
335
331
For more information on replica sets and write operations, see
336
332
:ref:`replica-set-write-concern`, :ref:`replica-set-oplog-sizing`,
337
333
:ref:`replica-set-oplog`, and :ref:`replica-set-procedure-change-oplog-size`.
338
334
339
- .. [#write-concern-throttling] By calling :dbcommand:`getLastError`
340
- intermittently with a ``w`` value of ``2`` or ``majority`` to
341
- :dbcommand:`getLastError` will slow the throughput of the write
342
- operation but will prevent bulk inserts.
335
+ .. [#write-concern-throttling] Calling :dbcommand:`getLastError`
336
+ intermittently with a ``w`` value of ``2`` or ``majority`` will slow
337
+ the throughput of the write operation but will prevent bulk inserts.
343
338
344
339
.. _write-operations-sharded-clusters:
345
340
0 commit comments