@@ -4,23 +4,29 @@ Write Operations
44
55.. default-domain:: mongodb
66
7- Synopsis
8- --------
7+ Write operations create, update, and delete data in MongoDB databases.
8+ MongoDB databases store data as :term:`documents <document>` in
9+ :term:`collections <collection>`.
10+
11+ This section of the manual describes how MongoDB performs write
12+ operations and how different factors affect the efficiency of those
13+ operations.
14+
15+ .. index:: write operators
16+ .. _write-operations-operators:
917
10- Operations
11- ----------
18+ Write Operators
19+ ---------------
1220
13- The :doc:`/crud` section of this manual contains specific
14- documentation for the major classes of write operations for MongoDB
15- databases. Read the following pages for additional examples and
16- documentation:
21+ For information on write operators and how to write data to a MongoDB
22+ database, see the following:
1723
18- :doc:`/applications/create`
19- :doc:`/applications/delete `
20- :doc:`/applications/update `
24+ - :doc:`/applications/create`
25+ - :doc:`/applications/update `
26+ - :doc:`/applications/delete `
2127
22- Also consider the following methods in the :program:`mongo` JavaScript
23- shell that allow you to write or change data in a MongoDB database.
28+ For information specific :program:`mongo` shell methods used in write
29+ operations, see the following:
2430
2531- :method:`db.collection.insert()`
2632- :method:`db.collection.update()`
@@ -29,43 +35,202 @@ shell that allow you to write or change data in a MongoDB database.
2935- :method:`db.collection.remove()`
3036- :method:`db.collection.delete()`
3137
32- Consider the documentation for your client library or :doc:`driver
33- </applications/drivers>` for more information on how to access this
34- functionality from within your application.
38+ For information on how to perform write operations from within an
39+ application, see the :doc:`/applications/drivers` documentation or the
40+ documentation for your client library.
41+
42+ .. index:: write concern
43+ .. _write-operations-write-concern:
44+
45+ Write Concern
46+ -------------
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+ :term:`Write concern <write concern>` confirms the success of write
52+ operations to MongoDB databases by returning an object indicating
53+ operational success. Beginning with version 2.4, the :program:`mongo`
54+ shell enables write concern by default. In previous versions the shell
55+ disabled write concern by default.
56+
57+ Many drivers also enable write concern by default. For information on
58+ your driver, see the :doc:`/applications/drivers` documentation.
59+
60+ .. todo add note about all drivers after `date` will have w:1 write
61+ concern for all operations by default.
62+
63+ 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.
68+
69+ A successful write operation means the :program:`mongod` instance
70+ received the write operation and has committed the operation to the
71+ in-memory representation of the database. This provides a simple and
72+ low-latency level of write concern and will allow your application to
73+ detect situations where the :program:`mongod` instance becomes
74+ inaccessible or insertion errors caused by :ref:`duplicate key errors
75+ <index-type-unique>`.
76+
77+ You can modify the level of write concern returned by issuing the
78+ :dbcommand:`getLastError` command with one or both of following options:
79+
80+ - ``j`` or "journal" option
81+
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.
86+
87+ - ``w`` option
88+
89+ 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.
93+
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>`.
99+
100+ To disable write concern, set the ``w`` option to ``0``, as shown in
101+ the following example:
102+
103+ .. code-block:: javascript
35104
36- Write Concern and Write Safety
37- ------------------------------
105+ db.runCommand( { getLastError: 1, w: 0 } )
38106
39- .. todo:: import and tweak section from the replica-set page. When we
40- publish this document we'll have to do a quick deletion/reduction
41- of the replica-set section, but during the editorial process the
42- content can be duplicated.
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.
110+
111+ .. _write-operations-bulk-insert:
43112
44113Bulk Inserts
45114------------
46115
47- :issue:`SERVER-2395`
116+ Bulk inserts let you insert many documents in a single database call.
117+
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.
122+
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.
126+
127+ Beginning with version 2.2, you also can perform bulk inserts through
128+ the :program:`mongo` shell.
129+
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.
136+
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.
141+
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.
144+
145+ Bulk inserts are often used with :term:`sharded collections <sharded
146+ collection>` and are more effective when the collection is already
147+ populated and MongoDB has already determined the key distribution. For
148+ more information on bulk inserts into sharded collections, see
149+ :ref:`sharding-bulk-inserts`.
150+
151+ If possible, consider using bulk inserts to insert event data.
48152
49- .. todo:: import the best content from: http://www.mongodb.org/display/DOCS/Bulk+Inserts sl
50- split between this section and the sharded clusters section.
153+ For more information, see :ref:`write-operations-sharded-clusters`,
154+ :ref:`sharding-bulk-inserts`, and :doc:`/administration/import-export`.
155+
156+ .. _write-operations-indexing:
51157
52158Indexing
53159--------
54160
55- .. todo:: short section on the impact of indexes and index maintenance
56- on write operations.
161+ After every insert, update, or delete operation, MongoDB updates not
162+ only the collection but also *every* index associated with the
163+ collection. Therefore, every index on a collection adds some amount of
164+ write-performance penalty.
165+
166+ In general, the performance gains that indexes realize for *read
167+ operations* are worth the insertion penalty. But if your application is
168+ write-heavy, be careful when creating new indexes.
169+
170+ For more information, see :doc:`/source/applications/indexes`.
171+
172+ .. _write-operations-isolation:
57173
58174Isolation
59175---------
60176
61- - atomicity
62- - :doc:`/tutorial/perform-two-phase-commits`
177+ All operations inside of a MongoDB document are atomic. An update
178+ operation may modify more than one document at more than one level
179+ (nesting) in a single operation that will either succeed or fail and
180+ cannot leave the document in an in-between state.
181+
182+ For more information see :doc:`Isolated write operations
183+ </reference/operator/atomic>` and
184+ :doc:`/tutorial/perform-two-phase-commits`.
63185
64186Architecture
65187------------
66188
189+ .. _write-operations-replica-sets:
190+
67191Replica Sets
68192~~~~~~~~~~~~
69193
194+ In :term:`replica sets <replica set>`, all write operations go to the
195+ set's :term:`primary`. MongoDB applies the write operations to the
196+ primary and then records the operations on the primary's :term:`oplog`.
197+ The :term:`secondary` members then replicate the oplog and apply the
198+ operations to themselves in an asynchronous process.
199+
200+ If you are performing a large data ingestion or bulk load operation that
201+ requires a large number of writes to the primary, the secondaries might
202+ not be able to read the oplog fast enough to keep up with changes. The
203+ oplog is a :term:`capped collection` and overwrites its oldest entries
204+ when it reaches a certain size. If the secondaries have not yet applied
205+ those entries because a large write operation has prevented them from
206+ reading the oplog, the secondaries will have fallen too far behind to
207+ catch up and will have become stale.
208+
209+ To prevent this, use :ref:`write concern
210+ <write-operations-write-concern>` to return write confirmation every
211+ 100, 1,000, or other designated number of operations. This provides an
212+ opportunity for secondaries to catch up with the primary. Write concern
213+ can slow the overall progress of write operations but prevents the
214+ secondaries from falling too far behind.
215+
216+ For more information on replica sets and write operations, see
217+ :ref:`replica-set-write-concern`, :ref:`replica-set-oplog-sizing`,
218+ :ref:`replica-set-oplog`,
219+ :ref:`replica-set-procedure-change-oplog-size`, and
220+ :ref:`replica-set-resync-stale-member`.
221+
222+ .. _write-operations-sharded-clustsers:
223+
70224Sharded Clusters
71225~~~~~~~~~~~~~~~~
226+
227+ In a :term:`sharded cluster`, MongoDB directs a given write operation to
228+ a :term:`shard` and then performs the write on a particular
229+ :term:`chunk` on that shard. Shards and chunks are range-based.
230+ :term:`Shard keys <shard keys>` affect how MongoDB distributes documents
231+ among shards. Choosing the correct shard key can have a great impact on
232+ the performance, capability, and functioning of your database and
233+ cluster.
234+
235+ For more information, see :doc:`/administration/sharding` and
236+ :ref:`write-operations-bulk-insert`.
0 commit comments