Skip to content

Commit d856c5d

Browse files
committed
Jerome feedback
1 parent dc7446c commit d856c5d

File tree

4 files changed

+65
-35
lines changed

4 files changed

+65
-35
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
// start-to-json
5+
function toJSON(object $document): string
6+
{
7+
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
8+
}
9+
// end-to-json
10+
11+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
12+
$client = new MongoDB\Client($uri);
13+
14+
// start-db-coll
15+
$collection = $client->sample_restaurants->restaurants;
16+
// end-db-coll
17+
18+
// Passes an options argument to watch() to include the post-image of updated documents
19+
// start-change-stream-post-image
20+
$options = ['fullDocument' => MongoDB\Operation\Watch::FULL_DOCUMENT_UPDATE_LOOKUP];
21+
$changeStream = $collection->watch([], $options);
22+
$changeStream->rewind();
23+
24+
do {
25+
$changeStream->next();
26+
27+
if ($changeStream->valid()) {
28+
$event = $changeStream->current();
29+
echo toJSON($event), PHP_EOL;
30+
}
31+
} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate');
32+
// end-change-stream-post-image

source/includes/read/change-streams.php renamed to source/includes/read/change-streams/change-stream-pipeline.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@ function toJSON(object $document): string
1515
$collection = $client->sample_restaurants->restaurants;
1616
// end-db-coll
1717

18-
// Monitors and prints changes to the "restaurants" collection
19-
// start-open-change-stream
20-
$changeStream = $collection->watch();
21-
$changeStream->rewind();
22-
23-
do {
24-
$changeStream->next();
25-
26-
if ($changeStream->valid()) {
27-
$event = $changeStream->current();
28-
echo toJSON($event), PHP_EOL;
29-
}
30-
} while {$event['operationType'] !== 'invalidate'};
31-
// end-open-change-stream
32-
3318
// Updates a document that has a "name" value of "Blarney Castle"
3419
// start-update-for-change-stream
3520
$result = $collection->updateOne(
@@ -51,23 +36,5 @@ function toJSON(object $document): string
5136
$event = $changeStream->current();
5237
echo toJSON($event), PHP_EOL;
5338
}
54-
55-
} while ($event['operationType'] !== 'invalidate');
39+
} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate');
5640
// end-change-stream-pipeline
57-
58-
// Passes an options argument to watch() to include the post-image of updated documents
59-
// start-change-stream-post-image
60-
$options = ['fullDocument' => MongoDB\Operation\Watch::FULL_DOCUMENT_UPDATE_LOOKUP];
61-
$changeStream = $collection->watch([], $options);
62-
$changeStream->rewind();
63-
64-
do {
65-
$changeStream->next();
66-
67-
if ($changeStream->valid()) {
68-
$event = $changeStream->current();
69-
echo toJSON($event), PHP_EOL;
70-
}
71-
} while ($event['operationType'] !== 'invalidate');
72-
// end-change-stream-post-image
73-
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
// start-to-json
5+
function toJSON(object $document): string
6+
{
7+
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
8+
}
9+
// end-to-json
10+
11+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
12+
$client = new MongoDB\Client($uri);
13+
14+
// start-db-coll
15+
$collection = $client->sample_restaurants->restaurants;
16+
// end-db-coll
17+
18+
// Monitors and prints changes to the "restaurants" collection
19+
// start-open-change-stream
20+
$changeStream = $collection->watch();
21+
$changeStream->rewind();
22+
23+
do {
24+
$changeStream->next();
25+
26+
if ($changeStream->valid()) {
27+
$event = $changeStream->current();
28+
echo toJSON($event), PHP_EOL;
29+
}
30+
} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate');
31+
// end-open-change-stream

source/includes/usage-examples/read-code-examples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@
6161
$event = $changeStream->current();
6262
echo toJSON($event) . PHP_EOL;
6363
}
64-
} while ($event['operationType'] !== 'invalidate');
64+
} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate');
6565
// end-change-stream

0 commit comments

Comments
 (0)