|
| 1 | +.. _php-bulk-write: |
| 2 | + |
| 3 | +===================== |
| 4 | +Bulk Write Operations |
| 5 | +===================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: insert, update, replace, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to perform multiple write operations |
| 24 | +in a single database call by using **bulk write operations**. |
| 25 | + |
| 26 | +Consider a scenario in which you want to insert a document into a collection, |
| 27 | +update multiple other documents, then delete a document. If you use |
| 28 | +individual methods, each operation requires its own database call. Instead, |
| 29 | +you can use a bulk operation to reduce the number of calls to the database. |
| 30 | + |
| 31 | +Sample Data |
| 32 | +~~~~~~~~~~~ |
| 33 | + |
| 34 | +The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` |
| 35 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection |
| 36 | +from your PHP application, instantiate a ``MongoDB\Client`` that connects to an Atlas cluster |
| 37 | +and assign the following value to your ``$collection`` variable: |
| 38 | + |
| 39 | +.. literalinclude:: /includes/write/bulk-write.php |
| 40 | + :language: php |
| 41 | + :dedent: |
| 42 | + :start-after: start-db-coll |
| 43 | + :end-before: end-db-coll |
| 44 | + |
| 45 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 46 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 47 | + |
| 48 | +.. _php-bulk-operations: |
| 49 | + |
| 50 | +Bulk Operations |
| 51 | +--------------- |
| 52 | + |
| 53 | +To run a bulk write operation, pass an array of write operations to the |
| 54 | +``MongoDB\Collection::bulkWrite()`` method. You can specify the following |
| 55 | +operations in the array: |
| 56 | + |
| 57 | +.. code-block:: php |
| 58 | + |
| 59 | + [ |
| 60 | + [ 'deleteMany' => [ $filter ] ], |
| 61 | + [ 'deleteOne' => [ $filter ] ], |
| 62 | + [ 'insertOne' => [ $document ] ], |
| 63 | + [ 'replaceOne' => [ $filter, $replacement, $options ] ], |
| 64 | + [ 'updateMany' => [ $filter, $update, $options ] ], |
| 65 | + [ 'updateOne' => [ $filter, $update, $options ] ], |
| 66 | + ] |
| 67 | + |
| 68 | +.. tip:: |
| 69 | + |
| 70 | + For more information about delete, insert, replace, and update |
| 71 | + operations, see the :ref:`Write operation guides <php-write>`. |
| 72 | + |
| 73 | +When you call the ``bulkWrite()`` method, the library automatically runs the |
| 74 | +write operations in the order they're specified in the array. To instruct ``bulkWrite()`` |
| 75 | +to run the write operations in an arbitrary order, see the :ref:`php-bulk-modify` section. |
| 76 | + |
| 77 | +Example |
| 78 | +~~~~~~~ |
| 79 | + |
| 80 | +The following example runs a delete, insert, and update operation on the |
| 81 | +``restaurants`` collection: |
| 82 | + |
| 83 | +.. literalinclude:: /includes/write/bulk-write.php |
| 84 | + :start-after: start-bulk-insert-one |
| 85 | + :end-before: end-bulk-insert-one |
| 86 | + :language: php |
| 87 | + :dedent: |
| 88 | + |
| 89 | +To insert multiple documents, create an instance of ``mongocxx::model::insert_one`` |
| 90 | +for each document. |
| 91 | + |
| 92 | +.. _php-bulk-update-model: |
| 93 | + |
| 94 | +Update Operations |
| 95 | +~~~~~~~~~~~~~~~~~ |
| 96 | + |
| 97 | +To update a document, create an instance of ``mongocxx::model::update_one``. This model |
| 98 | +instructs the driver to update *the first* document that matches your query filter. Then, |
| 99 | +append the model instance to an instance of the ``mongocxx::bulk_write`` class. |
| 100 | + |
| 101 | +Pass the following arguments to the ``mongocxx::model::update_one`` model: |
| 102 | + |
| 103 | +- **Query filter** document, which specifies the criteria used to match documents |
| 104 | + in your collection. |
| 105 | +- **Update** document, which specifies the kind of update to perform. For more information |
| 106 | + about update operations, see the :manual:`Field Update Operators |
| 107 | + </reference/operator/update-field/>` guide in the {+mdb-server+} manual. |
| 108 | + |
| 109 | +The following example creates an instance of ``mongocxx::model::update_one`` and appends |
| 110 | +it to a ``mongocxx::bulk_write`` instance called ``bulk``: |
| 111 | + |
| 112 | +.. literalinclude:: /includes/write/bulk-write.php |
| 113 | + :start-after: start-bulk-update-one |
| 114 | + :end-before: end-bulk-update-one |
| 115 | + :language: php |
| 116 | + :dedent: |
| 117 | + |
| 118 | +To update multiple documents, create an instance of ``mongocxx::model::update_many`` |
| 119 | +and pass in the same arguments. This model instructs the driver to update *all* documents |
| 120 | +that match your query filter. |
| 121 | + |
| 122 | +The following example creates an instance of ``mongocxx::model::update_many`` and appends |
| 123 | +it to ``bulk``: |
| 124 | + |
| 125 | +.. literalinclude:: /includes/write/bulk-write.php |
| 126 | + :start-after: start-bulk-update-many |
| 127 | + :end-before: end-bulk-update-many |
| 128 | + :language: php |
| 129 | + :dedent: |
| 130 | + |
| 131 | +.. _php-bulk-replace-model: |
| 132 | + |
| 133 | +Replace Operations |
| 134 | +~~~~~~~~~~~~~~~~~~ |
| 135 | + |
| 136 | +A replace operation removes all fields and values of a specified document and |
| 137 | +replaces them with new ones. To perform a replace operation, create an instance |
| 138 | +of the ``mongocxx::model::replace_one`` class and pass it a query filter and |
| 139 | +the fields and values you want to store in the matching document. Then, append |
| 140 | +the model instance to an instance of the ``mongocxx::bulk_write`` class. |
| 141 | + |
| 142 | +The following example creates an instance of ``mongocxx::model::replace_one`` and appends |
| 143 | +it to a ``mongocxx::bulk_write`` instance called ``bulk``: |
| 144 | + |
| 145 | +.. literalinclude:: /includes/write/bulk-write.php |
| 146 | + :start-after: start-bulk-replace-one |
| 147 | + :end-before: end-bulk-replace-one |
| 148 | + :language: php |
| 149 | + :dedent: |
| 150 | + |
| 151 | +To replace multiple documents, you must create a new instance of ``mongocxx::model::replace_one`` |
| 152 | +for each document. |
| 153 | + |
| 154 | +.. _php-bulk-delete-model: |
| 155 | + |
| 156 | +Delete Operations |
| 157 | +~~~~~~~~~~~~~~~~~ |
| 158 | + |
| 159 | +To delete a document, create an instance of the ``mongocxx::model::delete_one`` class and |
| 160 | +pass in a query filter specifying the document you want to delete. This model instructs |
| 161 | +the driver to delete only *the first* document that matches your query filter. Then, append |
| 162 | +the model instance to an instance of the ``mongocxx::bulk_write`` class. |
| 163 | + |
| 164 | +The following example creates an instance of ``mongocxx::model::delete_one`` and appends |
| 165 | +it to a ``mongocxx::bulk_write`` instance called ``bulk``: |
| 166 | + |
| 167 | +.. literalinclude:: /includes/write/bulk-write.php |
| 168 | + :start-after: start-bulk-delete-one |
| 169 | + :end-before: end-bulk-delete-one |
| 170 | + :language: php |
| 171 | + :dedent: |
| 172 | + |
| 173 | +To delete multiple documents, create an instance of the ``mongocxx::model::delete_many`` |
| 174 | +class and pass in a query filter specifying the document you want to delete. This model |
| 175 | +instructs the driver to delete *all* documents that match your query filter. |
| 176 | + |
| 177 | +The following example creates an instance of ``mongocxx::model::delete_many`` and appends |
| 178 | +it to ``bulk``: |
| 179 | + |
| 180 | +.. literalinclude:: /includes/write/bulk-write.php |
| 181 | + :start-after: start-bulk-delete-many |
| 182 | + :end-before: end-bulk-delete-many |
| 183 | + :language: php |
| 184 | + :dedent: |
| 185 | + |
| 186 | +.. _php-bulk-modify: |
| 187 | + |
| 188 | +Modify Bulk Write Behavior |
| 189 | +-------------------------- |
| 190 | + |
| 191 | +You can modify the behavior of the ``create_bulk_write()`` method by passing |
| 192 | +an instance of the ``mongocxx::options::bulk_write`` class as a parameter. The following |
| 193 | +table describes the fields you can set in a ``mongocxx::options::bulk_write`` |
| 194 | +instance: |
| 195 | + |
| 196 | +.. list-table:: |
| 197 | + :widths: 30 70 |
| 198 | + :header-rows: 1 |
| 199 | + |
| 200 | + * - Field |
| 201 | + - Description |
| 202 | + |
| 203 | + * - ``ordered`` |
| 204 | + - | If ``true``, the driver performs the write operations in the order |
| 205 | + provided. If an error occurs, the remaining operations are not |
| 206 | + attempted. |
| 207 | + | |
| 208 | + | If ``false``, the driver performs the operations in an |
| 209 | + arbitrary order and attempts to perform all operations. |
| 210 | + | Defaults to ``true``. |
| 211 | + |
| 212 | + * - ``bypass_document_validation`` |
| 213 | + - | Specifies whether the operation bypasses document-level validation. For more |
| 214 | + information, see :manual:`Schema |
| 215 | + Validation </core/schema-validation/#schema-validation>` in the MongoDB |
| 216 | + Server manual. |
| 217 | + | Defaults to ``false``. |
| 218 | + |
| 219 | + * - ``write_concern`` |
| 220 | + - | Specifies the write concern for the bulk operation. For more information, see |
| 221 | + :manual:`Write Concern </reference/write-concern/>` in the {+mdb-server+} manual. |
| 222 | + |
| 223 | + * - ``comment`` |
| 224 | + - | Attaches a comment to the operation. For more information, see the :manual:`delete command |
| 225 | + fields </reference/command/delete/#command-fields>` guide in the |
| 226 | + {+mdb-server+} manual. |
| 227 | + |
| 228 | + * - ``let`` |
| 229 | + - | Specifies a document with a list of values to improve operation readability. Values |
| 230 | + must be constant or closed expressions that don't reference document fields. For more |
| 231 | + information, see the :manual:`let statement |
| 232 | + </reference/command/delete/#std-label-delete-let-syntax>` in the {+mdb-server+} manual. |
| 233 | + |
| 234 | +The following example calls the ``create_bulk_write()`` method from the |
| 235 | +:ref:`php-bulk-start-operation` example on this page, but sets the ``ordered`` field |
| 236 | +of a ``mongocxx::options::bulk_write`` instance to ``false``: |
| 237 | + |
| 238 | +.. literalinclude:: /includes/write/bulk-write.php |
| 239 | + :start-after: start-bulk-write-unordered |
| 240 | + :end-before: end-bulk-write-unordered |
| 241 | + :language: php |
| 242 | + :dedent: |
| 243 | + |
| 244 | +If any of the write operations in an unordered bulk write fail, the {+driver-short+} |
| 245 | +reports the errors only after attempting all operations. |
| 246 | + |
| 247 | +.. note:: |
| 248 | + |
| 249 | + Unordered bulk operations do not guarantee order of execution. The order can |
| 250 | + differ from the way you list them to optimize the runtime. |
| 251 | + |
| 252 | +.. _php-bulk-return-value: |
| 253 | + |
| 254 | +Return Value |
| 255 | +------------ |
| 256 | + |
| 257 | +The ``execute()`` method returns an instance of the ``mongocxx::result::bulk_write`` class. |
| 258 | +The ``mongocxx::result::bulk_write`` class contains the following member functions: |
| 259 | + |
| 260 | +.. list-table:: |
| 261 | + :widths: 30 70 |
| 262 | + :header-rows: 1 |
| 263 | + |
| 264 | + * - Function |
| 265 | + - Description |
| 266 | + |
| 267 | + * - ``deleted_count()`` |
| 268 | + - | Returns the number of documents deleted, if any. |
| 269 | + |
| 270 | + * - ``inserted_count()`` |
| 271 | + - | Returns the number of documents inserted, if any. |
| 272 | + |
| 273 | + * - ``matched_count()`` |
| 274 | + - | Returns the number of documents matched for an update, if applicable. |
| 275 | + |
| 276 | + * - ``modified_count()`` |
| 277 | + - | Returns the number of documents modified, if any. |
| 278 | + |
| 279 | + * - ``upserted_count()`` |
| 280 | + - | Returns the number of documents upserted, if any. |
| 281 | + |
| 282 | + * - ``upserted_ids()`` |
| 283 | + - | Returns a map of the operation's index to the ``_id`` of the upserted documents, if |
| 284 | + applicable. |
| 285 | + |
| 286 | +Additional Information |
| 287 | +---------------------- |
| 288 | + |
| 289 | +To learn how to perform individual write operations, see the following guides: |
| 290 | + |
| 291 | +- :ref:`php-write-insert` |
| 292 | +- :ref:`php-write-update` |
| 293 | +- :ref:`php-write-delete` |
| 294 | + |
| 295 | +.. TODO: |
| 296 | + - :ref:`php-write-replace` |
| 297 | + |
| 298 | +API Documentation |
| 299 | +~~~~~~~~~~~~~~~~~ |
| 300 | + |
| 301 | +To learn more about any of the methods or types discussed in this |
| 302 | +guide, see the following API documentation: |
| 303 | + |
| 304 | +- `create_bulk_write() <{+api+}/classmongocxx_1_1v__noabi_1_1collection.html#abbf0932175201384cc902c80740adfdc>`__ |
| 305 | +- `mongocxx::model::insert_one <{+api+}/classmongocxx_1_1v__noabi_1_1model_1_1insert__one.html>`__ |
| 306 | +- `mongocxx::model::update_one <{+api+}/classmongocxx_1_1v__noabi_1_1model_1_1update__one.html>`__ |
| 307 | +- `mongocxx::model::update_many <{+api+}/classmongocxx_1_1v__noabi_1_1model_1_1update__many.html>`__ |
| 308 | +- `mongocxx::model::replace_one <{+api+}/classmongocxx_1_1v__noabi_1_1model_1_1replace__one.html>`__ |
| 309 | +- `mongocxx::model::delete_one <{+api+}/classmongocxx_1_1v__noabi_1_1model_1_1delete__one.html>`__ |
| 310 | +- `mongocxx::model::delete_many <{+api+}/classmongocxx_1_1v__noabi_1_1model_1_1delete__many.html>`__ |
| 311 | +- `execute() <{+api+}/classmongocxx_1_1v__noabi_1_1bulk__write.html#a13476d87ed6d00dca52c39dc04b98568>`__ |
| 312 | +- `mongocxx::options::bulk_write <{+api+}/classmongocxx_1_1v__noabi_1_1options_1_1bulk__write.html>`__ |
| 313 | +- `mongocxx::result::bulk_write <{+api+}/classmongocxx_1_1v__noabi_1_1result_1_1bulk__write.html>`__ |
0 commit comments