Skip to content

Commit 063a59b

Browse files
committed
DOCSP-49150: bulkWrite docs + api
1 parent 9f93381 commit 063a59b

19 files changed

+1308
-55
lines changed

source/includes/extracts-bulkwriteexception.yaml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ref: bulkwriteexception-result
22
content: |
33
If a :php:`MongoDB\Driver\Exception\BulkWriteException
44
<mongodb-driver-exception-bulkwriteexception>` is thrown, users should call
5-
:php:`getWriteResult() <mongodb-driver-writeexception.getwriteresult>` and
5+
:php:`getWriteResult() <mongodb-driver-bulkwriteexception.getwriteresult>` and
66
inspect the returned :php:`MongoDB\Driver\WriteResult
77
<mongodb-driver-writeresult>` object to determine the nature of the error.
88
@@ -11,11 +11,22 @@ content: |
1111
too long). Alternatively, a write operation may have failed outright (e.g.
1212
unique key violation).
1313
---
14+
ref: bulkwriteexception-client-result
15+
content: |
16+
If a :php:`MongoDB\Driver\Exception\BulkWriteCommandException
17+
<mongodb-driver-exception-bulkwritecommandexception>` is thrown, users should call
18+
:php:`getWriteErrors() <mongodb-driver-exception-bulkwritecommandexception.getwriteerrors>` and
19+
inspect the information in the returned array to determine the nature of the error.
20+
21+
For example, a write operation may have been successfully applied to the
22+
primary server but failed to satisfy the write concern. Alternatively, a
23+
write operation may have failed outright, for example for violating the
24+
unique key constraint.
25+
---
1426
ref: bulkwriteexception-ordered
1527
content: |
1628
In the case of a bulk write, the result may indicate multiple successful write
1729
operations and/or errors. If the ``ordered`` option is ``true``, some
1830
operations may have succeeded before the first error was encountered and the
1931
exception thrown. If the ``ordered`` option is ``false``, multiple errors may
2032
have been encountered.
21-
...

source/includes/extracts-error.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ content: |
33
:php:`MongoDB\Driver\Exception\BulkWriteException
44
<mongodb-driver-exception-bulkwriteexception>` for errors related to the write
55
operation. Users should inspect the value returned by :php:`getWriteResult()
6-
<mongodb-driver-writeexception.getwriteresult>` to determine the nature of the
6+
<mongodb-driver-bulkwriteexception.getwriteresult>` to determine the nature of the
7+
error.
8+
---
9+
ref: error-driver-client-bulkwriteexception
10+
content: |
11+
:php:`MongoDB\Driver\Exception\BulkWriteCommandException
12+
<mongodb-driver-exception-bulkwritecommandexception>` for errors related to the write
13+
operation. Users should inspect the value returned by :php:`getWriteErrors()
14+
<mongodb-driver-exception-bulkwritecommandexception.getwriteerrors>` to determine the nature of the
715
error.
816
---
917
ref: error-driver-invalidargumentexception
@@ -49,4 +57,3 @@ ref: error-gridfs-corruptfileexception
4957
content: |
5058
:phpclass:`MongoDB\GridFS\Exception\CorruptFileException` if the file's
5159
metadata or chunk documents contain unexpected or invalid data.
52-
...

source/includes/write/bulk-write.php

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@
55
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
66
$client = new MongoDB\Client($uri);
77

8-
// start-db-coll
9-
$collection = $client->sample_restaurants->restaurants;
10-
// end-db-coll
11-
128
// start-run-bulk
13-
$result = $collection->bulkWrite(
9+
$restaurantCollection = $client->sample_restaurants->restaurants;
10+
11+
$result = $restaurantCollection->bulkWrite(
1412
[
1513
[
1614
'insertOne' => [
17-
['name' => 'Mongo\'s Deli'],
18-
['cuisine' => 'Sandwiches'],
19-
['borough' => 'Manhattan'],
20-
['restaurant_id' => '1234'],
15+
['name' => 'Mongo\'s Deli'],
16+
['cuisine' => 'Sandwiches'],
17+
['borough' => 'Manhattan'],
18+
['restaurant_id' => '1234'],
2119
],
2220
],
2321
[
2422
'updateOne' => [
25-
['name' => 'Mongo\'s Deli'],
23+
['name' => 'Mongo\'s Deli'],
2624
['$set' => ['cuisine' => 'Sandwiches and Salads']],
2725
],
2826
],
@@ -36,14 +34,14 @@
3634
// end-run-bulk
3735

3836
// start-bulk-options
39-
$result = $collection->bulkWrite(
37+
$result = $restaurantCollection->bulkWrite(
4038
[
4139
[
4240
'insertOne' => [
43-
['name' => 'Mongo\'s Pizza'],
44-
['cuisine' => 'Italian'],
45-
['borough' => 'Queens'],
46-
['restaurant_id' => '5678'],
41+
['name' => 'Mongo\'s Pizza'],
42+
['cuisine' => 'Italian'],
43+
['borough' => 'Queens'],
44+
['restaurant_id' => '5678'],
4745
],
4846
],
4947
[
@@ -55,3 +53,95 @@
5553
['ordered' => false]
5654
);
5755
// end-bulk-options
56+
57+
// start-bulk-client-insert-one
58+
$restaurantCollection = $client->sample_restaurants->restaurants;
59+
$movieCollection = $client->sample_mflix->movies;
60+
61+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection);
62+
$bulkWrite->insertOne(['name' => 'Mongo Deli', 'cuisine' => 'Sandwiches']);
63+
64+
$bulkWrite = MongoDB\ClientBulkWrite::withCollection($movieCollection);
65+
$bulkWrite->insertOne(['title' => 'The Green Ray', 'year' => 1986]);
66+
// end-bulk-client-insert-one
67+
68+
// start-bulk-client-update-one
69+
$restaurantCollection = $client->sample_restaurants->restaurants;
70+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection);
71+
72+
$bulkWrite->updateOne(
73+
['name' => 'Dandelion Bakery'],
74+
['$set' => ['grade' => 'B+']],
75+
['upsert' => true],
76+
);
77+
// end-bulk-client-update-one
78+
79+
// start-bulk-client-update-many
80+
$restaurantCollection = $client->sample_restaurants->restaurants;
81+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection);
82+
83+
$bulkWrite->updateMany(
84+
['name' => 'Starbucks'],
85+
['$set' => ['cuisine' => 'Coffee (Chain)']],
86+
);
87+
// end-bulk-client-update-many
88+
89+
// start-bulk-client-replace-one
90+
$restaurantCollection = $client->sample_restaurants->restaurants;
91+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection);
92+
93+
$bulkWrite->replaceOne(
94+
['name' => 'Dandelion Bakery'],
95+
['name' => 'Flower Patisserie', 'cuisine' => 'Bakery & Cafe'],
96+
);
97+
// end-bulk-client-replace-one
98+
99+
// start-bulk-client-delete-one
100+
$restaurantCollection = $client->sample_restaurants->restaurants;
101+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection);
102+
103+
$bulkWrite->deleteOne(
104+
['borough' => 'Queens'],
105+
);
106+
// end-bulk-client-delete-one
107+
108+
// start-bulk-client-delete-many
109+
$restaurantCollection = $client->sample_restaurants->restaurants;
110+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection);
111+
112+
$bulkWrite->deleteMany(
113+
['name' => ['$regex' => 'p{2,}']],
114+
);
115+
// end-bulk-client-delete-many
116+
117+
// start-bulk-client
118+
$restaurantCollection = $client->sample_restaurants->restaurants;
119+
$movieCollection = $client->sample_mflix->movies;
120+
121+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection);
122+
$bulkWrite->insertOne(['name' => 'Mongo Deli', 'cuisine' => 'Sandwiches']);
123+
$bulkWrite->updateOne(
124+
['name' => 'Dandelion Bakery'],
125+
['$set' => ['grade' => 'B+']],
126+
['upsert' => true],
127+
);
128+
129+
$bulkWrite = MongoDB\ClientBulkWrite::withCollection($movieCollection);
130+
$bulkWrite->insertOne(['title' => 'The Green Ray', 'year' => 1986]);
131+
$bulkWrite->deleteMany(
132+
['title' => ['$regex' => 'd{2,}']],
133+
);
134+
$bulkWrite->replaceOne(
135+
['runtime' => ['$gte' => 200]],
136+
['title' => 'Seven Samurai', 'runtime' => 203],
137+
);
138+
139+
$result = $client->bulkWrite($bulkWrite);
140+
echo 'Inserted documents: ', $result->getInsertedCount(), PHP_EOL;
141+
echo 'Modified documents: ', $result->getModifiedCount(), PHP_EOL;
142+
echo 'Deleted documents: ', $result->getDeletedCount(), PHP_EOL;
143+
// end-bulk-client
144+
145+
// start-bulk-client-options
146+
$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($collection, ['ordered' => false]);
147+
// end-bulk-client-options

source/reference.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ API Documentation
1111

1212
BSON </reference/bson>
1313
MongoDB\Client </reference/class/MongoDBClient>
14+
MongoDB\ClientBulkWrite </reference/class/MongoDBClientBulkWrite>
1415
MongoDB\Database </reference/class/MongoDBDatabase>
1516
MongoDB\Collection </reference/class/MongoDBCollection>
1617
MongoDB\GridFS\Bucket </reference/class/MongoDBGridFSBucket>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
=====================================
2+
MongoDB\\BulkWriteCommandResult Class
3+
=====================================
4+
5+
Definition
6+
----------
7+
8+
.. phpclass:: MongoDB\BulkWriteCommandResult
9+
10+
This class contains information about an executed client bulk write operation. It
11+
is returned from :phpmethod:`MongoDB\Client::bulkWrite()`.
12+
13+
Methods
14+
-------
15+
16+
.. list-table::
17+
:widths: 30 70
18+
:header-rows: 1
19+
20+
* - Method
21+
- Description
22+
23+
* - ``getInsertedCount()``
24+
- | Returns the number of documents inserted, if any.
25+
26+
* - ``getMatchedCount()``
27+
- | Returns the number of documents matched during update and replace
28+
operations, if applicable.
29+
30+
* - ``getModifiedCount()``
31+
- | Returns the number of documents modified, if any.
32+
33+
* - ``getUpsertedCount()``
34+
- | Returns the number of documents upserted, if any.
35+
36+
* - ``getDeletedCount()``
37+
- | Returns the number of documents deleted, if any.
38+
39+
* - ``getInsertResults()``
40+
- | Returns a map of results of each successful insert operation. Each
41+
operation is represented by an integer key, which contains a
42+
document with information corresponding to the operation such
43+
as the inserted ``_id`` value.
44+
45+
* - ``getUpdateResults()``
46+
- | Returns a map of results of each successful update operation. Each
47+
operation is represented by an integer key, which contains a
48+
document with information corresponding to the operation.
49+
50+
* - ``getDeleteResults()``
51+
- | Returns a map of results of each successful delete operation.
52+
Each operation is represented by an integer key, which contains
53+
a document with information corresponding to the operation.
54+
55+
* - ``isAcknowledged()``
56+
- | Returns a boolean indicating whether the bulk operation was acknowledged.
57+
58+
See Also
59+
--------
60+
61+
- :ref:`php-bulk-write`

source/reference/class/MongoDBClient.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Methods
3232
__construct() </reference/method/MongoDBClient__construct>
3333
__get() </reference/method/MongoDBClient__get>
3434
addSubscriber() </reference/method/MongoDBClient-addSubscriber>
35+
bulkWrite() </reference/method/MongoDBClient-bulkWrite>
3536
createClientEncryption() </reference/method/MongoDBClient-createClientEncryption>
3637
dropDatabase() </reference/method/MongoDBClient-dropDatabase>
3738
getCollection() </reference/method/MongoDBClient-getCollection>
@@ -52,6 +53,7 @@ Methods
5253
- :phpmethod:`MongoDB\Client::__construct()`
5354
- :phpmethod:`MongoDB\Client::__get()`
5455
- :phpmethod:`MongoDB\Client::addSubscriber()`
56+
- :phpmethod:`MongoDB\Client::bulkWrite()`
5557
- :phpmethod:`MongoDB\Client::createClientEncryption()`
5658
- :phpmethod:`MongoDB\Client::dropDatabase()`
5759
- :phpmethod:`MongoDB\Client::getCollection()`
@@ -67,4 +69,4 @@ Methods
6769
- :phpmethod:`MongoDB\Client::selectCollection()`
6870
- :phpmethod:`MongoDB\Client::selectDatabase()`
6971
- :phpmethod:`MongoDB\Client::startSession()`
70-
- :phpmethod:`MongoDB\Client::watch()`
72+
- :phpmethod:`MongoDB\Client::watch()`
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
==============================
2+
MongoDB\\ClientBulkWrite Class
3+
==============================
4+
5+
.. versionadded:: 2.1
6+
7+
Definition
8+
----------
9+
10+
.. phpclass:: MongoDB\ClientBulkWrite
11+
12+
This class allows you to assemble a bulk write command to pass to
13+
:phpmethod:`MongoDB\Client::bulkWrite()`.
14+
15+
This class allows you to specify write operations across multiple
16+
namespaces in the same cluster.
17+
18+
Methods
19+
-------
20+
21+
.. toctree::
22+
:titlesonly:
23+
24+
createWithCollection() </reference/method/MongoDBClientBulkWrite-createWithCollection>
25+
deleteMany() </reference/method/MongoDBClientBulkWrite-deleteMany>
26+
deleteOne() </reference/method/MongoDBClientBulkWrite-deleteOne>
27+
insertOne() </reference/method/MongoDBClientBulkWrite-insertOne>
28+
replaceOne() </reference/method/MongoDBClientBulkWrite-replaceOne>
29+
updateMany() </reference/method/MongoDBClientBulkWrite-updateMany>
30+
updateOne() </reference/method/MongoDBClientBulkWrite-updateOne>
31+
withCollection() </reference/method/MongoDBClientBulkWrite-withCollection>
32+
33+
- :phpmethod:`MongoDB\ClientBulkWrite::createWithCollection()`
34+
- :phpmethod:`MongoDB\ClientBulkWrite::deleteMany()`
35+
- :phpmethod:`MongoDB\ClientBulkWrite::deleteOne()`
36+
- :phpmethod:`MongoDB\ClientBulkWrite::insertOne()`
37+
- :phpmethod:`MongoDB\ClientBulkWrite::replaceOne()`
38+
- :phpmethod:`MongoDB\ClientBulkWrite::updateMany()`
39+
- :phpmethod:`MongoDB\ClientBulkWrite::updateOne()`
40+
- :phpmethod:`MongoDB\ClientBulkWrite::withCollection()`
41+
42+
See Also
43+
--------
44+
45+
- :ref:`php-bulk-write`

0 commit comments

Comments
 (0)