|
1 | 1 | <?php |
2 | | -require 'vendor/autoload.php'; |
| 2 | +require 'vendor/autoload.php'; |
3 | 3 |
|
4 | 4 | $uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); |
5 | 5 | $client = new MongoDB\Client($uri); |
6 | 6 | $collection = $client->db->coll; |
| 7 | +$anotherCollection = $client->db->collection; |
7 | 8 |
|
8 | 9 | // Inserts one document that stores the specified values |
9 | 10 | // start-insert-one |
|
63 | 64 | $result = $collection->deleteMany(['<field name>' => '<value>']); |
64 | 65 | // end-delete-multiple |
65 | 66 |
|
66 | | -// Runs a bulk operation based on the instructions in each array entry |
| 67 | +// Runs a bulk operation based on the operations in the ClientBulkWrite object |
67 | 68 | // start-bulk-write |
68 | | -$result = $collection->bulkWrite( |
69 | | - [ |
70 | | - [ |
71 | | - 'insertOne' => [ |
72 | | - ['<field name>' => '<value>'], |
73 | | - ], |
74 | | - ], |
75 | | - [ |
76 | | - 'replaceOne' => [ |
77 | | - ['<field to match>' => '<value to match>'], |
78 | | - [ |
79 | | - '<first new field>' => '<value>', |
80 | | - '<second new field>' => '<value>', |
81 | | - ], |
82 | | - ], |
83 | | - ], |
84 | | - [ |
85 | | - 'updateOne' => [ |
86 | | - ['<field to match>' => '<value to match>'], |
87 | | - ['$set' => ['<field to update>' => '<value to update>']], |
88 | | - ], |
89 | | - ], |
90 | | - [ |
91 | | - 'updateMany' => [ |
92 | | - ['<field to match>' => '<value to match>'], |
93 | | - ['$set' => ['<field to update>' => '<value to update>']], |
94 | | - ], |
95 | | - ], |
96 | | - [ |
97 | | - 'deleteOne' => [ |
98 | | - ['<field name>' => '<value>'], |
99 | | - ], |
100 | | - ], |
101 | | - [ |
102 | | - 'deleteMany' => [ |
103 | | - ['<field name>' => '<value>'], |
104 | | - ], |
105 | | - ], |
106 | | - ] |
| 69 | +$bulkWrite = MongoDB\ClientBulkWrite::createWithCollection($collection); |
| 70 | + |
| 71 | +$bulkWrite->insertOne(['<field name 1>' => '<value 1>', '<field name 2>' => '<value 2>']); |
| 72 | + |
| 73 | +$bulkWrite->updateOne( |
| 74 | + ['<field to match>' => '<value to match>'], |
| 75 | + ['$set' => ['<field to update>' => '<updated value>']], |
107 | 76 | ); |
| 77 | + |
| 78 | +$bulkWrite = $bulkWrite->withCollection($anotherCollection); |
| 79 | + |
| 80 | +$bulkWrite->deleteMany( |
| 81 | + ['<field name>' => '<value>'], |
| 82 | +); |
| 83 | + |
| 84 | +$bulkWrite->replaceOne( |
| 85 | + ['<field to match>' => '<value to match>'], |
| 86 | + ['<replacement field 1>' => '<replacement value 1>', '<replacement field 2>' => '<replacement value 2>'], |
| 87 | +); |
| 88 | + |
| 89 | +$result = $client->bulkWrite($bulkWrite); |
108 | 90 | // end-bulk-write |
109 | 91 |
|
110 | 92 | // Stores a file in a GridFS bucket and writes data to the file |
|
0 commit comments