|  | 
| 5 | 5 | $uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | 
| 6 | 6 | $client = new MongoDB\Client($uri); | 
| 7 | 7 | 
 | 
| 8 |  | -// start-db-coll | 
| 9 |  | -$collection = $client->sample_restaurants->restaurants; | 
| 10 |  | -// end-db-coll | 
| 11 |  | - | 
| 12 | 8 | // start-run-bulk | 
| 13 |  | -$result = $collection->bulkWrite( | 
|  | 9 | +$restaurantCollection = $client->sample_restaurants->restaurants; | 
|  | 10 | + | 
|  | 11 | +$result = $restaurantCollection->bulkWrite( | 
| 14 | 12 |     [ | 
| 15 | 13 |         [ | 
| 16 | 14 |             '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'], | 
| 21 | 19 |             ], | 
| 22 | 20 |         ], | 
| 23 | 21 |         [ | 
| 24 | 22 |             'updateOne' => [ | 
| 25 |  | -                ['name' => 'Mongo\'s Deli'],  | 
|  | 23 | +                ['name' => 'Mongo\'s Deli'], | 
| 26 | 24 |                 ['$set' => ['cuisine' => 'Sandwiches and Salads']], | 
| 27 | 25 |             ], | 
| 28 | 26 |         ], | 
|  | 
| 36 | 34 | // end-run-bulk | 
| 37 | 35 | 
 | 
| 38 | 36 | // start-bulk-options | 
| 39 |  | -$result = $collection->bulkWrite( | 
|  | 37 | +$result = $restaurantCollection->bulkWrite( | 
| 40 | 38 |     [ | 
| 41 | 39 |         [ | 
| 42 | 40 |             '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'], | 
| 47 | 45 |             ], | 
| 48 | 46 |         ], | 
| 49 | 47 |         [ | 
|  | 
| 55 | 53 |     ['ordered' => false] | 
| 56 | 54 | ); | 
| 57 | 55 | // 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 = $bulkWrite->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 | +// Creates the bulk write command and sets the target namespace. | 
|  | 121 | +$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($restaurantCollection); | 
|  | 122 | +// Specifies insertion of one document. | 
|  | 123 | +$bulkWrite->insertOne(['name' => 'Mongo Deli', 'cuisine' => 'Sandwiches']); | 
|  | 124 | +// Specifies a `$set` update to one document with the upsert option | 
|  | 125 | +// enabled. | 
|  | 126 | +$bulkWrite->updateOne( | 
|  | 127 | +    ['name' => 'Dandelion Bakery'], | 
|  | 128 | +    ['$set' => ['grade' => 'B+']], | 
|  | 129 | +    ['upsert' => true], | 
|  | 130 | +); | 
|  | 131 | +// Changes the target namespace. | 
|  | 132 | +$bulkWrite = $bulkWrite->withCollection($movieCollection); | 
|  | 133 | +// Specifies insertion of one document. | 
|  | 134 | +$bulkWrite->insertOne(['title' => 'The Green Ray', 'year' => 1986]); | 
|  | 135 | +// Specifies deletion of documents in which `title` has two consective | 
|  | 136 | +// 'd' characters. | 
|  | 137 | +$bulkWrite->deleteMany( | 
|  | 138 | +    ['title' => ['$regex' => 'd{2,}']], | 
|  | 139 | +); | 
|  | 140 | +// Specifies replacement of one document. | 
|  | 141 | +$bulkWrite->replaceOne( | 
|  | 142 | +    ['runtime' => ['$gte' => 200]], | 
|  | 143 | +    ['title' => 'Seven Samurai', 'runtime' => 203], | 
|  | 144 | +); | 
|  | 145 | + | 
|  | 146 | +// Performs the bulk write operation. | 
|  | 147 | +$result = $client->bulkWrite($bulkWrite); | 
|  | 148 | +// Prints a summary of results. | 
|  | 149 | +echo 'Inserted documents: ', $result->getInsertedCount(), PHP_EOL; | 
|  | 150 | +echo 'Modified documents: ', $result->getModifiedCount(), PHP_EOL; | 
|  | 151 | +echo 'Deleted documents: ', $result->getDeletedCount(), PHP_EOL; | 
|  | 152 | +// end-bulk-client | 
|  | 153 | + | 
|  | 154 | +// start-bulk-client-options | 
|  | 155 | +$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection( | 
|  | 156 | +    $restaurantCollection, | 
|  | 157 | +    ['ordered' => false] | 
|  | 158 | +); | 
|  | 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