|
117 | 117 | // start-bulk-client |
118 | 118 | $restaurantCollection = $client->sample_restaurants->restaurants; |
119 | 119 | $movieCollection = $client->sample_mflix->movies; |
120 | | - |
| 120 | +// Creates the bulk write command and sets the target namespace. |
121 | 121 | $bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection); |
| 122 | +// Specifies insertion of one document. |
122 | 123 | $bulkWrite->insertOne(['name' => 'Mongo Deli', 'cuisine' => 'Sandwiches']); |
| 124 | +// Specifies a `$set` update to one document with the upsert option |
| 125 | +// enabled. |
123 | 126 | $bulkWrite->updateOne( |
124 | 127 | ['name' => 'Dandelion Bakery'], |
125 | 128 | ['$set' => ['grade' => 'B+']], |
126 | 129 | ['upsert' => true], |
127 | 130 | ); |
128 | | - |
| 131 | +// Changes the target namespace. |
129 | 132 | $bulkWrite = $bulkWrite->withCollection($movieCollection); |
| 133 | +// Specifies insertion of one document. |
130 | 134 | $bulkWrite->insertOne(['title' => 'The Green Ray', 'year' => 1986]); |
| 135 | +// Specifies deletion of documents in which `title` has two consective |
| 136 | +// 'd' characters. |
131 | 137 | $bulkWrite->deleteMany( |
132 | 138 | ['title' => ['$regex' => 'd{2,}']], |
133 | 139 | ); |
| 140 | +// Specifies replacement of one document. |
134 | 141 | $bulkWrite->replaceOne( |
135 | 142 | ['runtime' => ['$gte' => 200]], |
136 | 143 | ['title' => 'Seven Samurai', 'runtime' => 203], |
137 | 144 | ); |
138 | 145 |
|
| 146 | +// Performs the bulk write operation. |
139 | 147 | $result = $client->bulkWrite($bulkWrite); |
| 148 | +// Prints a summary of results. |
140 | 149 | echo 'Inserted documents: ', $result->getInsertedCount(), PHP_EOL; |
141 | 150 | echo 'Modified documents: ', $result->getModifiedCount(), PHP_EOL; |
142 | 151 | echo 'Deleted documents: ', $result->getDeletedCount(), PHP_EOL; |
143 | 152 | // end-bulk-client |
144 | 153 |
|
145 | 154 | // start-bulk-client-options |
146 | | -$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($collection, ['ordered' => false]); |
| 155 | +$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection( |
| 156 | + $restaurantCollection, |
| 157 | + ['ordered' => false] |
| 158 | +); |
147 | 159 | // end-bulk-client-options |
| 160 | + |
| 161 | +// start-bulk-client-unordered-behavior |
| 162 | +$bulkWrite->insertOne(['_id' => 4045, 'title' => 'The Green Ray']); |
| 163 | +$bulkWrite->deleteOne(['_id' => 4045]); |
| 164 | +// end-bulk-client-unordered-behavior |
0 commit comments