-
Notifications
You must be signed in to change notification settings - Fork 34
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
Merged
mcmorisi
merged 7 commits into
mongodb:master
from
mcmorisi:DOCSP-43912-fix-monitor-code
Jun 25, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab88a1f
DOCSP-43912: Improve change stream code examples
mcmorisi dc7446c
Fix
mcmorisi d856c5d
Jerome feedback
mcmorisi 12588df
Fix
mcmorisi 3caa44c
PHP team feedback
mcmorisi a36e707
Forgot a file
mcmorisi dbe3a01
Fix
mcmorisi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
source/includes/read/change-streams/change-stream-options.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
// start-to-json | ||
function toJSON(object $document): string | ||
{ | ||
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON(); | ||
} | ||
// end-to-json | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
// start-db-coll | ||
$collection = $client->sample_restaurants->restaurants; | ||
// end-db-coll | ||
|
||
// 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(); | ||
|
||
do { | ||
$changeStream->next(); | ||
|
||
if ($changeStream->valid()) { | ||
$event = $changeStream->current(); | ||
echo toJSON($event), PHP_EOL; | ||
} | ||
} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate'); | ||
// end-change-stream-post-image |
40 changes: 40 additions & 0 deletions
40
source/includes/read/change-streams/change-stream-pipeline.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
// start-to-json | ||
function toJSON(object $document): string | ||
{ | ||
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON(); | ||
} | ||
// end-to-json | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
// start-db-coll | ||
$collection = $client->sample_restaurants->restaurants; | ||
// end-db-coll | ||
|
||
// Updates a document that has a "name" value of "Blarney Castle" | ||
// start-update-for-change-stream | ||
$result = $collection->updateOne( | ||
['name' => 'Blarney Castle'], | ||
['$set' => ['cuisine' => 'Irish']] | ||
); | ||
// end-update-for-change-stream | ||
|
||
// Passes a pipeline argument to watch() to monitor only update operations | ||
// start-change-stream-pipeline | ||
$pipeline = [['$match' => ['operationType' => 'update']]]; | ||
$changeStream = $collection->watch($pipeline); | ||
$changeStream->rewind(); | ||
|
||
do { | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$changeStream->next(); | ||
|
||
if ($changeStream->valid()) { | ||
$event = $changeStream->current(); | ||
echo toJSON($event), PHP_EOL; | ||
} | ||
} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate'); | ||
// end-change-stream-pipeline |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
// start-to-json | ||
function toJSON(object $document): string | ||
{ | ||
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON(); | ||
} | ||
// end-to-json | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
// start-db-coll | ||
$collection = $client->sample_restaurants->restaurants; | ||
// end-db-coll | ||
|
||
// Monitors and prints changes to the "restaurants" collection | ||
// start-open-change-stream | ||
$changeStream = $collection->watch(); | ||
$changeStream->rewind(); | ||
|
||
do { | ||
$changeStream->next(); | ||
|
||
if ($changeStream->valid()) { | ||
$event = $changeStream->current(); | ||
echo toJSON($event), PHP_EOL; | ||
} | ||
} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate'); | ||
// end-open-change-stream | ||
|
||
// Updates a document that has a "name" value of "Blarney Castle" | ||
// start-update-for-change-stream | ||
$result = $collection->updateOne( | ||
['name' => 'Blarney Castle'], | ||
['$set' => ['cuisine' => 'Irish']] | ||
); | ||
// end-update-for-change-stream | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.