Skip to content

Commit ad6d94e

Browse files
committed
Add examples for bulk writes and with_transaction
1 parent 46e7cb2 commit ad6d94e

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

examples/bulk.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MongoDB\Examples;
5+
6+
use MongoDB\Client;
7+
8+
use function assert;
9+
use function dirname;
10+
use function getenv;
11+
use function is_object;
12+
use function MongoDB\BSON\fromPHP;
13+
use function MongoDB\BSON\toRelaxedExtendedJSON;
14+
use function printf;
15+
16+
require dirname(__FILE__) . '/../vendor/autoload.php';
17+
18+
function toJSON(object $document): string
19+
{
20+
return toRelaxedExtendedJSON(fromPHP($document));
21+
}
22+
23+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
24+
25+
$collection = $client->test->coll;
26+
$collection->drop();
27+
28+
$documents = [];
29+
30+
for ($i = 0; $i < 10; $i++) {
31+
$documents[] = ['x' => $i];
32+
}
33+
34+
$collection->insertMany($documents);
35+
36+
$collection->bulkWrite(
37+
[
38+
[
39+
'deleteMany' => [
40+
['x' => ['$gt' => 7]], // Filter
41+
],
42+
],
43+
[
44+
'deleteOne' => [
45+
['x' => 4], // Filter
46+
],
47+
],
48+
[
49+
'replaceOne' => [
50+
['x' => 1], // Filter
51+
['y' => 1], // Replacement
52+
],
53+
],
54+
[
55+
'updateMany' => [
56+
['x' => ['$gt' => 5]], // Filter
57+
['$set' => ['updateMany' => true]], // Update
58+
],
59+
],
60+
[
61+
'updateOne' => [
62+
['x' => 2], // Filter
63+
['$set' => ['y' => 2]], // Update
64+
],
65+
],
66+
[
67+
'insertOne' => [
68+
['x' => 10], // Document
69+
],
70+
],
71+
]
72+
);
73+
74+
$cursor = $collection->find([], ['batchSize' => 2]);
75+
76+
foreach ($cursor as $document) {
77+
assert(is_object($document));
78+
printf("%s\n", toJSON($document));
79+
}

examples/with_transaction.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MongoDB\Examples;
5+
6+
use MongoDB\Client;
7+
use MongoDB\Driver\Session;
8+
9+
use function assert;
10+
use function dirname;
11+
use function getenv;
12+
use function is_object;
13+
use function MongoDB\BSON\fromPHP;
14+
use function MongoDB\BSON\toRelaxedExtendedJSON;
15+
use function MongoDB\with_transaction;
16+
use function printf;
17+
18+
require dirname(__FILE__) . '/../vendor/autoload.php';
19+
20+
function toJSON(object $document): string
21+
{
22+
return toRelaxedExtendedJSON(fromPHP($document));
23+
}
24+
25+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
26+
27+
$collection = $client->test->coll;
28+
$collection->drop();
29+
30+
$insertData = function (Session $session) use ($collection): void {
31+
$collection->insertMany(
32+
[
33+
['x' => 1],
34+
['x' => 2],
35+
['x' => 3],
36+
],
37+
['session' => $session]
38+
);
39+
40+
$collection->updateMany(
41+
['x' => ['$gt' => 1]],
42+
['$set' => ['y' => 1]],
43+
['session' => $session]
44+
);
45+
};
46+
47+
$session = $client->startSession();
48+
49+
with_transaction($session, $insertData);
50+
51+
$cursor = $collection->find([], ['batchSize' => 2]);
52+
53+
foreach ($cursor as $document) {
54+
assert(is_object($document));
55+
printf("%s\n", toJSON($document));
56+
}

0 commit comments

Comments
 (0)