Skip to content

Commit d0a12bd

Browse files
committed
edits
1 parent 2f05bac commit d0a12bd

File tree

4 files changed

+135
-159
lines changed

4 files changed

+135
-159
lines changed

source/includes/write/bulk-write.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6+
$client = new MongoDB\Client($uri);
7+
8+
// start-db-coll
9+
$collection = $client->sample_restaurants->restaurants;
10+
// end-db-coll
11+
12+
// start-run-bulk
13+
$result = $collection->bulkWrite(
14+
[
15+
[
16+
'insertOne' => [
17+
['name' => 'Mongo\'s Deli'],
18+
['cuisine' => 'Sandwiches'],
19+
['borough' => 'Manhattan'],
20+
['restaurant_id' => '1234'],
21+
],
22+
],
23+
[
24+
'updateOne' => [
25+
['name' => 'Mongo\'s Deli'],
26+
['$set' => ['cuisine' => 'Sandwiches and Salads']],
27+
],
28+
],
29+
[
30+
'deleteMany' => [
31+
['borough' => 'Manhattan'],
32+
],
33+
],
34+
]
35+
);
36+
// end-run-bulk
37+
38+
// start-bulk-options
39+
$result = $collection->bulkWrite(
40+
[
41+
[
42+
'insertOne' => [
43+
['name' => 'Mongo\'s Pizza'],
44+
['cuisine' => 'Italian'],
45+
['borough' => 'Queens'],
46+
['restaurant_id' => '5678'],
47+
],
48+
],
49+
[
50+
'deleteOne' => [
51+
['restaurant_id' => '5678'],
52+
],
53+
],
54+
],
55+
['ordered' => false]
56+
);
57+
// end-bulk-options

source/tutorial/codecs.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _php-codecs:
2+
13
======
24
Codecs
35
======

source/write.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Write Data to MongoDB
88
:titlesonly:
99
:maxdepth: 1
1010

11-
/write/update
11+
/write/update
12+
/write/bulk-write

source/write/bulk-write.txt

Lines changed: 74 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Bulk Operations
5151
---------------
5252

5353
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:
5656

5757
.. code-block:: php
5858

@@ -77,109 +77,21 @@ to run the write operations in an arbitrary order, see the :ref:`php-bulk-modify
7777
Example
7878
~~~~~~~
7979

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

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'``
9685

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'``
10088

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'``
11191

11292
.. 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
18395
:language: php
18496
:dedent:
18597

@@ -188,61 +100,69 @@ it to ``bulk``:
188100
Modify Bulk Write Behavior
189101
--------------------------
190102

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

196107
.. list-table::
197108
:widths: 30 70
198109
:header-rows: 1
199110

200-
* - Field
111+
* - Option
201112
- Description
202113

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
215118
Validation </core/schema-validation/#schema-validation>` in the MongoDB
216119
Server manual.
217120
| Defaults to ``false``.
218121

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``.
222145

223146
* - ``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
226149
{+mdb-server+} manual.
227150

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

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``:
237157

238158
.. 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
241161
:language: php
242162
:dedent:
243163

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

247167
.. note::
248168

@@ -254,8 +174,8 @@ reports the errors only after attempting all operations.
254174
Return Value
255175
------------
256176

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

260180
.. list-table::
261181
:widths: 30 70
@@ -264,24 +184,30 @@ The ``mongocxx::result::bulk_write`` class contains the following member functio
264184
* - Function
265185
- Description
266186

267-
* - ``deleted_count()``
187+
* - ``getDeletedCount()``
268188
- | Returns the number of documents deleted, if any.
269189

270-
* - ``inserted_count()``
190+
* - ``getInsertedCount()``
271191
- | Returns the number of documents inserted, if any.
272192

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

276-
* - ``modified_count()``
196+
* - ``getMatchedCount()``
197+
- | Returns the number of documents matched during update and replace
198+
operations, if applicable.
199+
200+
* - ``getModifiedCount()``
277201
- | Returns the number of documents modified, if any.
278202

279-
* - ``upserted_count()``
203+
* - ``getUpsertedCount()``
280204
- | Returns the number of documents upserted, if any.
281205

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

286212
Additional Information
287213
----------------------
@@ -291,23 +217,13 @@ To learn how to perform individual write operations, see the following guides:
291217
- :ref:`php-write-insert`
292218
- :ref:`php-write-update`
293219
- :ref:`php-write-delete`
294-
295-
.. TODO:
296-
- :ref:`php-write-replace`
220+
- :ref:`php-write-replace`
297221

298222
API Documentation
299223
~~~~~~~~~~~~~~~~~
300224

301225
To learn more about any of the methods or types discussed in this
302226
guide, see the following API documentation:
303227

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

Comments
 (0)