@@ -51,8 +51,8 @@ Bulk Operations
51
51
---------------
52
52
53
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 :
54
+ ``MongoDB\Collection::bulkWrite()`` method. Use the following syntax to
55
+ specify the write operations :
56
56
57
57
.. code-block:: php
58
58
@@ -77,109 +77,21 @@ to run the write operations in an arbitrary order, see the :ref:`php-bulk-modify
77
77
Example
78
78
~~~~~~~
79
79
80
- The following example runs a delete, insert, and update operation on the
81
- ``restaurants`` collection:
80
+ This example runs the following write operations on the ``restaurants``
81
+ collection:
82
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
- ~~~~~~~~~~~~~~~~~
83
+ - Insert operation, which inserts a document in which the ``name`` value is
84
+ ``'Mongo's Deli'``
96
85
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.
86
+ - Update operation, which updates the ``cuisine`` field of a document in
87
+ which the ``name`` value is ``'Mongo's Deli'``
100
88
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``:
89
+ - Delete operation, which deletes all documents in which the ``borough`` value
90
+ is ``'Manhattan'``
111
91
112
92
.. 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
93
+ :start-after: start-run-bulk
94
+ :end-before: end-run-bulk
183
95
:language: php
184
96
:dedent:
185
97
@@ -188,61 +100,69 @@ it to ``bulk``:
188
100
Modify Bulk Write Behavior
189
101
--------------------------
190
102
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:
103
+ You can modify the behavior of the ``MongoDB\Collection::bulkWrite()`` method by
104
+ passing an array that specifies option values as a parameter. The following table
105
+ describes the options you can set in the array:
195
106
196
107
.. list-table::
197
108
:widths: 30 70
198
109
:header-rows: 1
199
110
200
- * - Field
111
+ * - Option
201
112
- Description
202
113
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
114
+ * - ``bypassDocumentValidation``
115
+ - | Specifies whether the operation bypasses document validation. This lets you
116
+ modify documents that don't meet the schema validation requirements, if any
117
+ exist. For more information about schema validation, see :manual:`Schema
215
118
Validation </core/schema-validation/#schema-validation>` in the MongoDB
216
119
Server manual.
217
120
| Defaults to ``false``.
218
121
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.
122
+ * - ``codec``
123
+ - | Sets the codec to use for encoding or decoding documents. Bulk writes
124
+ use the codec for ``insertOne()`` and ``replaceOne()`` operations.
125
+ For more information, see the :ref:`php-codecs` guide.
126
+
127
+ * - ``writeConcern``
128
+ - | Sets the write concern for the operation.
129
+ For more information, see :manual:`Write Concern </reference/write-concern/>`
130
+ in the {+mdb-server+} manual.
131
+
132
+ * - ``let``
133
+ - | Specifies a document with a list of values to improve operation readability.
134
+ Values must be constant or closed expressions that don't reference document
135
+ fields. For more information, see the :manual:`let statement
136
+ </reference/command/update/#std-label-update-let-syntax>` in the
137
+ {+mdb-server+} manual.
138
+
139
+ * - ``ordered``
140
+ - | If set to ``true``: when a single write fails, the operation stops without
141
+ performing the remaining writes and throws an exception.
142
+ | If set to ``false``: when a single write fails, the operation continues to
143
+ attempt the remaining write operations, if any, and throws an exception.
144
+ | Defaults to ``true``.
222
145
223
146
* - ``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
147
+ - | Attaches a comment to the operation. For more information, see the :manual:`insert command
148
+ fields </reference/command/insert /#command-fields>` guide in the
226
149
{+mdb-server+} manual.
227
150
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.
151
+ * - ``session``
152
+ - | Specifies the client session to associate with the operation.
233
153
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``:
154
+ The following example calls the ``bulkWrite ()`` method to perform an
155
+ insert and delete operation and sets the ``ordered`` option to
156
+ ``false``:
237
157
238
158
.. literalinclude:: /includes/write/bulk-write.php
239
- :start-after: start-bulk-write-unordered
240
- :end-before: end-bulk-write-unordered
159
+ :start-after: start-bulk-options
160
+ :end-before: end-bulk-options
241
161
:language: php
242
162
:dedent:
243
163
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.
164
+ If the library runs the insert operation first, one document is deleted.
165
+ If it runs the delete operation first, no documents are deleted.
246
166
247
167
.. note::
248
168
@@ -254,8 +174,8 @@ reports the errors only after attempting all operations.
254
174
Return Value
255
175
------------
256
176
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:
177
+ The ``MongoDB\Collection::bulkWrite ()`` method returns a ``MongoDB\BulkWriteResult``
178
+ object. This class contains the following member functions:
259
179
260
180
.. list-table::
261
181
:widths: 30 70
@@ -264,24 +184,30 @@ The ``mongocxx::result::bulk_write`` class contains the following member functio
264
184
* - Function
265
185
- Description
266
186
267
- * - ``deleted_count ()``
187
+ * - ``getDeletedCount ()``
268
188
- | Returns the number of documents deleted, if any.
269
189
270
- * - ``inserted_count ()``
190
+ * - ``getInsertedCount ()``
271
191
- | Returns the number of documents inserted, if any.
272
192
273
- * - ``matched_count ()``
274
- - | Returns the number of documents matched for an update , if applicable .
193
+ * - ``getInsertedIds ()``
194
+ - | Returns a map of ``_id`` field values for inserted documents , if any .
275
195
276
- * - ``modified_count()``
196
+ * - ``getMatchedCount()``
197
+ - | Returns the number of documents matched during update and replace
198
+ operations, if applicable.
199
+
200
+ * - ``getModifiedCount()``
277
201
- | Returns the number of documents modified, if any.
278
202
279
- * - ``upserted_count ()``
203
+ * - ``getUpsertedCount ()``
280
204
- | Returns the number of documents upserted, if any.
281
205
282
- * - ``upserted_ids()``
283
- - | Returns a map of the operation's index to the ``_id`` of the upserted documents, if
284
- applicable.
206
+ * - ``getUpsertedIds()``
207
+ - | Returns a map of ``_id`` field values for upserted documents, if any.
208
+
209
+ * - ``isAcknowledged()``
210
+ - | Returns a boolean indicating whether the bulk operation was acknowledged.
285
211
286
212
Additional Information
287
213
----------------------
@@ -291,23 +217,13 @@ To learn how to perform individual write operations, see the following guides:
291
217
- :ref:`php-write-insert`
292
218
- :ref:`php-write-update`
293
219
- :ref:`php-write-delete`
294
-
295
- .. TODO:
296
- - :ref:`php-write-replace`
220
+ - :ref:`php-write-replace`
297
221
298
222
API Documentation
299
223
~~~~~~~~~~~~~~~~~
300
224
301
225
To learn more about any of the methods or types discussed in this
302
226
guide, see the following API documentation:
303
227
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>`__
228
+ - `MongoDB\\Collection::bulkWrite() <{+api+}/method/MongoDBCollection-bulkWrite>`__
229
+ - `MongoDB\\BulkWriteResult <{+api+}/class/MongoDBBulkWriteResult>`__
0 commit comments