Skip to content

Commit 452811c

Browse files
author
Bob Grabar
committed
DOCS-249 write operations: draft 2
1 parent 995d1af commit 452811c

File tree

1 file changed

+76
-56
lines changed

1 file changed

+76
-56
lines changed

draft/core/write-operations.txt

Lines changed: 76 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,23 @@ documentation for your client library.
4545
Write Concern
4646
-------------
4747

48-
.. DONE The information on the replica-set page has now been split between
49-
this page and the replica-set page.
50-
5148
:term:`Write concern <write concern>` confirms the success of write
5249
operations to MongoDB databases by returning an object indicating
5350
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.
5655

57-
Many drivers also enable write concern by default. For information on
58-
your driver, see the :doc:`/applications/drivers` documentation.
5956

6057
.. todo add note about all drivers after `date` will have w:1 write
6158
concern for all operations by default.
6259

6360
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.
6865

6966
A successful write operation means the :program:`mongod` instance
7067
received the write operation and has committed the operation to the
@@ -74,81 +71,104 @@ detect situations where the :program:`mongod` instance becomes
7471
inaccessible or detect insertion errors caused by :ref:`duplicate key errors
7572
<index-type-unique>`.
7673

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+
7778
You can modify the level of write concern returned by issuing the
7879
:dbcommand:`getLastError` command with one or both of following options:
7980

8081
- ``j`` or "journal" option
8182

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" } )
8691

8792
- ``w`` option
8893

8994
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.
9399

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:
99101

100-
To disable write concern, set the ``w`` option to ``0``, as shown in
101-
the following example:
102+
- ``-1``
102103

103-
.. code-block:: javascript
104+
Turns off reporting of network errors.
104105

105-
db.runCommand( { getLastError: 1, w: 0 } )
106+
- ``0``
106107

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.
110109

111-
.. _write-operations-bulk-insert:
110+
.. note:: If you disable write concern but enable the journal
111+
option, as shown here:
112112

113-
Bulk Inserts
114-
------------
113+
.. code-block:: javascript
115114

116-
Bulk inserts let you insert many documents in a single database call.
115+
{ getLastError: 1, w: 0, j: "true" }
117116

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.
122119

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``
126121

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.
129124

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+
------------
136145

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.
141149

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.
144153

145154
Bulk inserts are often used with :term:`sharded collections <sharded
146155
collection>` and are more effective when the collection is already
147156
populated and MongoDB has already determined the key distribution. For
148157
more information on bulk inserts into sharded collections, see
149158
:ref:`sharding-bulk-inserts`.
150159

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.
152172

153173
For more information, see :ref:`write-operations-sharded-clusters`,
154174
:ref:`sharding-bulk-inserts`, and :doc:`/administration/import-export`.

0 commit comments

Comments
 (0)