Skip to content

Commit ab88a1f

Browse files
committed
DOCSP-43912: Improve change stream code examples
1 parent a187970 commit ab88a1f

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

source/includes/read/change-streams.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ function toJSON(object $document): string
1818
// Monitors and prints changes to the "restaurants" collection
1919
// start-open-change-stream
2020
$changeStream = $collection->watch();
21+
$changeStream->rewind();
2122

22-
for ($changeStream->rewind(); true; $changeStream->next()) {
23-
if ( ! $changeStream->valid()) {
24-
continue;
25-
}
26-
$event = $changeStream->current();
27-
echo toJSON($event), PHP_EOL;
23+
do {
24+
$changeStream->next();
2825

29-
if ($event['operationType'] === 'invalidate') {
30-
break;
26+
if ($changeStream->valid()) {
27+
$event = $changeStream->current();
28+
echo toJSON($event), PHP_EOL;
3129
}
32-
}
30+
} while {$event['operationType'] !== 'invalidate'};
3331
// end-open-change-stream
3432

3533
// Updates a document that has a "name" value of "Blarney Castle"
@@ -44,35 +42,32 @@ function toJSON(object $document): string
4442
// start-change-stream-pipeline
4543
$pipeline = [['$match' => ['operationType' => 'update']]];
4644
$changeStream = $collection->watch($pipeline);
45+
$changeStream->rewind();
4746

48-
for ($changeStream->rewind(); true; $changeStream->next()) {
49-
if ( ! $changeStream->valid()) {
50-
continue;
51-
}
52-
$event = $changeStream->current();
53-
echo toJSON($event), PHP_EOL;
47+
do {
48+
$changeStream->next();
5449

55-
if ($event['operationType'] === 'invalidate') {
56-
break;
50+
if ($changeStream->valid()) {
51+
$event = $changeStream->current();
52+
echo toJSON($event), PHP_EOL;
5753
}
58-
}
54+
55+
} while ($event['operationType'] !== 'invalidate');
5956
// end-change-stream-pipeline
6057

6158
// Passes an options argument to watch() to include the post-image of updated documents
6259
// start-change-stream-post-image
6360
$options = ['fullDocument' => MongoDB\Operation\Watch::FULL_DOCUMENT_UPDATE_LOOKUP];
6461
$changeStream = $collection->watch([], $options);
62+
$changeStream->rewind();
6563

66-
for ($changeStream->rewind(); true; $changeStream->next()) {
67-
if ( ! $changeStream->valid()) {
68-
continue;
69-
}
70-
$event = $changeStream->current();
71-
echo toJSON($event), PHP_EOL;
64+
do {
65+
$changeStream->next();
7266

73-
if ($event['operationType'] === 'invalidate') {
74-
break;
67+
if ($changeStream->valid()) {
68+
$event = $changeStream->current();
69+
echo toJSON($event), PHP_EOL;
7570
}
76-
}
71+
} while ($event['operationType'] !== 'invalidate');
7772
// end-change-stream-post-image
7873

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,14 @@
5151

5252
// Data Changes
5353
// start-change-stream
54-
$changeStream = $collection->watch();
54+
$changeStream->rewind();
5555

56-
for ($changeStream->rewind(); true; $changeStream->next()) {
57-
if ( ! $changeStream->valid()) {
58-
continue;
59-
}
60-
$event = $changeStream->current();
61-
echo toJSON($event), PHP_EOL;
56+
do {
57+
$changeStream->next();
6258

63-
if ($event['operationType'] === 'invalidate') {
64-
break;
59+
if ($changeStream->valid()) {
60+
$event = $changeStream->current();
61+
echo toJSON($event) . PHP_EOL;
6562
}
66-
}
63+
} while ($event['operationType'] !== 'invalidate');
6764
// end-change-stream

0 commit comments

Comments
 (0)