Skip to content

DOCSP-43912: Improve change stream code examples #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions source/includes/read/change-streams.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ function toJSON(object $document): string
// Monitors and prints changes to the "restaurants" collection
// start-open-change-stream
$changeStream = $collection->watch();
$changeStream->rewind();

for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
do {
$changeStream->next();

if ($event['operationType'] === 'invalidate') {
break;
if ($changeStream->valid()) {
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
}
}
} while {$event['operationType'] !== 'invalidate'};
// end-open-change-stream

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

for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
do {
$changeStream->next();

if ($event['operationType'] === 'invalidate') {
break;
if ($changeStream->valid()) {
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
}
}

} while ($event['operationType'] !== 'invalidate');
// end-change-stream-pipeline

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

for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
do {
$changeStream->next();

if ($event['operationType'] === 'invalidate') {
break;
if ($changeStream->valid()) {
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
}
}
} while ($event['operationType'] !== 'invalidate');
// end-change-stream-post-image

16 changes: 7 additions & 9 deletions source/includes/usage-examples/read-code-examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@
// Data Changes
// start-change-stream
$changeStream = $collection->watch();
$changeStream->rewind();

for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
do {
$changeStream->next();

if ($event['operationType'] === 'invalidate') {
break;
if ($changeStream->valid()) {
$event = $changeStream->current();
echo toJSON($event) . PHP_EOL;
}
}
} while ($event['operationType'] !== 'invalidate');
// end-change-stream
Loading