|
| 1 | +<page xmlns="http://projectmallard.org/1.0/" |
| 2 | + type="topic" |
| 3 | + id="bulk"> |
| 4 | + <title>Bulk Write Operations</title> |
| 5 | + |
| 6 | + <p>This tutorial explains how to take advantage of MongoDB C driver bulk write operation features. Executing write operations in batches reduces the number of network round trips, increasing write throughput.</p> |
| 7 | + |
| 8 | + <section id="bulk-insert"> |
| 9 | + <info><link xref="index#bulk" type="guide"/></info> |
| 10 | + <title>Bulk Insert</title> |
| 11 | + <p><em>New in MongoDB C driver 0.94.2.</em></p> |
| 12 | + <p>First we need to fetch a bulk operation handle from the <link xref="mongoc_collection_t">mongoc_collection_t</link>. This can be performed in either ordered or unordered mode. Unordered mode allows for greater parallelization when working with sharded clusters.</p> |
| 13 | + <synopsis><code mime="text/x-csrc"><![CDATA[mongoc_bulk_operation_t *bulk = mongoc_collection_create_bulk_operation (collection, true, write_concern);]]></code></synopsis> |
| 14 | + <p>We can now start inserting documents to the bulk operation. These will be buffered until we execute the operation.</p> |
| 15 | + <p>The bulk operation will coalesce insertions as a single batch for each consecutive call to <link xref="mongoc_bulk_operation_insert">mongoc_bulk_operation_insert()</link>. This creates a pipelined effect when possible.</p> |
| 16 | + <note style="tip"><p>The bulk operation API will automatically handle MongoDB servers < 2.6 by speaking the old wire protocol. However, some performance degredation may occur.</p></note> |
| 17 | + <p>To execute the bulk operation and receive the result we call <link xref="mongoc_bulk_operation_execute">mongoc_bulk_operation_execute()</link>.</p> |
| 18 | + <synopsis><code mime="text/x-csrc"><include parse="text" href="../examples/bulk/bulk1.c" xmlns="http://www.w3.org/2001/XInclude" /></code></synopsis> |
| 19 | + <synopsis><code mime="text/x-json">{"nInserted" : 10000, |
| 20 | + "nMatched" : 0, |
| 21 | + "nModified" : 0, |
| 22 | + "nRemoved" : 0, |
| 23 | + "nUpserted" : 0, |
| 24 | + "writeErrors" : []}</code></synopsis> |
| 25 | + </section> |
| 26 | + |
| 27 | + <section id="mixed-bulk-write"> |
| 28 | + <info><link xref="index#bulk" type="guide"/></info> |
| 29 | + <title>Mixed Bulk Write Operations</title> |
| 30 | + <p><em>New in MongoDB C driver 0.94.2</em></p> |
| 31 | + <p>MongoDB C driver also supports executing mixed bulk write operations. A batch of insert, update, and remove operations can be executed together using the bulk write operations API.</p> |
| 32 | + <note style="tip"><p>Though the following API will work with all versions of MongoDB, it is designed to be used with MongoDB versions >= 2.6. Much better bulk insert performance can be achieved with older versions of MongoDB through the deprecated <link xref="mongoc_collection_insert_bulk">mongoc_collection_insert_bulk()</link> method.</p></note> |
| 33 | + </section> |
| 34 | + |
| 35 | + <section id="ordered-bulk-write"> |
| 36 | + <info><link xref="index#bulk" type="guide"/></info> |
| 37 | + <title>Ordered Bulk Write Operations</title> |
| 38 | + <p>Ordered bulk write operations are batched and sent to the server in the order provided for serial execution. The return value is a document describing the type and count of operations performed.</p> |
| 39 | + <synopsis><code mime="text/x-csrc"><include parse="text" href="../examples/bulk/bulk2.c" xmlns="http://www.w3.org/2001/XInclude" /></code></synopsis> |
| 40 | + <synopsis><code mime="text/x-json"><![CDATA[{ "nInserted" : 3, |
| 41 | + "nMatched" : 2, |
| 42 | + "nModified" : 2, |
| 43 | + "nRemoved" : 10000, |
| 44 | + "nUpserted" : 1, |
| 45 | + "upserted" : [{"index" : 5, "_id" : 4}], |
| 46 | + "writeErrors" : [] }]]></code></synopsis> |
| 47 | + <note style="warning"><p><code>nModified</code> is only reported by MongoDB 2.6 and later. When connected to an earlier server version, or in certain mixed version sharding configurations, MongoDB C driver omits this field from the results of a bulk write operation.</p></note> |
| 48 | + </section> |
| 49 | + |
| 50 | + <section id="unordered-bulk-write"> |
| 51 | + <info><link xref="index#bulk" type="guide"/></info> |
| 52 | + <title>Unordered Bulk Write Operations</title> |
| 53 | + <p>Unordered bulk write operations are batched and sent to the server in <em>arbitrary order</em> where they may be executed in parallel. Any errors that occur are reported after all operations are attempted.</p> |
| 54 | + <p>In the next example the first and third operations fail due to the unique constraint on <code>_id</code>. Since we are doing unordered execution the second and fourth operations succeed.</p> |
| 55 | + <synopsis><code mime="text/x-csrc"><include parse="text" href="../examples/bulk/bulk3.c" xmlns="http://www.w3.org/2001/XInclude" /></code></synopsis> |
| 56 | + <synopsis><code mime="text/x-csrc"><![CDATA[{ "nInserted" : 0, |
| 57 | + "nMatched" : 1, |
| 58 | + "nModified" : 1, |
| 59 | + "nRemoved" : 1, |
| 60 | + "nUpserted" : 0, |
| 61 | + "writeErrors" : [{ "index": 0, |
| 62 | + "code": 11000, |
| 63 | + "errmsg": "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.test.$_id_ dup key: { : 1 }" }, |
| 64 | + { "index" : 2, |
| 65 | + "code" : 11000, |
| 66 | + "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.test.$_id_ dup key: { : 3 }" }] } |
| 67 | +Error: insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.test.$_id_ dup key: { : 1 }]]></code></synopsis> |
| 68 | + </section> |
| 69 | + |
| 70 | + <section id="bulk-write-concern"> |
| 71 | + <info><link xref="index#bulk" type="guide"/></info> |
| 72 | + <title>Bulk Operations Write Concerns</title> |
| 73 | + <p>By default bulk operations are executed with the <link xref="mongoc_write_concern_t">write_concern</link> of the collection they are executed against. A custom write concern can be passed to the <link xref="mongoc_collection_create_bulk_operation">mongoc_collection_create_bulk_operation()</link> method. Write concern errors (e.g. wtimeout) will be reported after all operations are attempted, regardless of execution order.</p> |
| 74 | + </section> |
| 75 | + |
| 76 | +</page> |
0 commit comments