From ab88a1ffd7f0491bc4031f24a7ae21cf8d06fd59 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 4 Jun 2025 13:54:48 -0400 Subject: [PATCH 1/4] DOCSP-43912: Improve change stream code examples --- source/includes/read/change-streams.php | 49 +++++++++---------- .../usage-examples/read-code-examples.php | 17 +++---- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/source/includes/read/change-streams.php b/source/includes/read/change-streams.php index b00ced02..93d334ff 100644 --- a/source/includes/read/change-streams.php +++ b/source/includes/read/change-streams.php @@ -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" @@ -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 diff --git a/source/includes/usage-examples/read-code-examples.php b/source/includes/usage-examples/read-code-examples.php index d0db63b9..40d1a107 100644 --- a/source/includes/usage-examples/read-code-examples.php +++ b/source/includes/usage-examples/read-code-examples.php @@ -51,17 +51,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 From dc7446c7c25960e5415594428b8f17e453bf14cd Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 4 Jun 2025 14:13:28 -0400 Subject: [PATCH 2/4] Fix --- source/includes/usage-examples/read-code-examples.php | 1 + 1 file changed, 1 insertion(+) diff --git a/source/includes/usage-examples/read-code-examples.php b/source/includes/usage-examples/read-code-examples.php index 40d1a107..6aafdc4a 100644 --- a/source/includes/usage-examples/read-code-examples.php +++ b/source/includes/usage-examples/read-code-examples.php @@ -51,6 +51,7 @@ // Data Changes // start-change-stream +$changeStream = $collection->watch(); $changeStream->rewind(); do { From d856c5d036a20f5f355947b59431ab42f0a654b9 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Tue, 10 Jun 2025 09:46:00 -0400 Subject: [PATCH 3/4] Jerome feedback --- .../change-streams/change-stream-options.php | 32 +++++++++++++++++ .../change-stream-pipeline.php} | 35 +------------------ .../change-streams/open-change-stream.php | 31 ++++++++++++++++ .../usage-examples/read-code-examples.php | 2 +- 4 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 source/includes/read/change-streams/change-stream-options.php rename source/includes/read/{change-streams.php => change-streams/change-stream-pipeline.php} (54%) create mode 100644 source/includes/read/change-streams/open-change-stream.php diff --git a/source/includes/read/change-streams/change-stream-options.php b/source/includes/read/change-streams/change-stream-options.php new file mode 100644 index 00000000..2a2aaf85 --- /dev/null +++ b/source/includes/read/change-streams/change-stream-options.php @@ -0,0 +1,32 @@ +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 diff --git a/source/includes/read/change-streams.php b/source/includes/read/change-streams/change-stream-pipeline.php similarity index 54% rename from source/includes/read/change-streams.php rename to source/includes/read/change-streams/change-stream-pipeline.php index 93d334ff..b01de35a 100644 --- a/source/includes/read/change-streams.php +++ b/source/includes/read/change-streams/change-stream-pipeline.php @@ -15,21 +15,6 @@ function toJSON(object $document): string $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 {$event['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( @@ -51,23 +36,5 @@ function toJSON(object $document): string $event = $changeStream->current(); echo toJSON($event), PHP_EOL; } - -} while ($event['operationType'] !== 'invalidate'); +} while (! $changeStream->valid() || $changeStream->current()['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(); - -do { - $changeStream->next(); - - if ($changeStream->valid()) { - $event = $changeStream->current(); - echo toJSON($event), PHP_EOL; - } -} while ($event['operationType'] !== 'invalidate'); -// end-change-stream-post-image - diff --git a/source/includes/read/change-streams/open-change-stream.php b/source/includes/read/change-streams/open-change-stream.php new file mode 100644 index 00000000..1b42977d --- /dev/null +++ b/source/includes/read/change-streams/open-change-stream.php @@ -0,0 +1,31 @@ +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 diff --git a/source/includes/usage-examples/read-code-examples.php b/source/includes/usage-examples/read-code-examples.php index 6aafdc4a..8833ecb7 100644 --- a/source/includes/usage-examples/read-code-examples.php +++ b/source/includes/usage-examples/read-code-examples.php @@ -61,5 +61,5 @@ $event = $changeStream->current(); echo toJSON($event) . PHP_EOL; } -} while ($event['operationType'] !== 'invalidate'); +} while (! $changeStream->valid() || $changeStream->current()['operationType'] !== 'invalidate'); // end-change-stream From 12588dfa21f930f8a64f5bbe5124eaa3db938ef8 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Tue, 10 Jun 2025 09:54:05 -0400 Subject: [PATCH 4/4] Fix --- .../{open-change-stream.php => change-stream.php} | 8 ++++++++ source/read/change-streams.txt | 12 ++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) rename source/includes/read/change-streams/{open-change-stream.php => change-stream.php} (78%) diff --git a/source/includes/read/change-streams/open-change-stream.php b/source/includes/read/change-streams/change-stream.php similarity index 78% rename from source/includes/read/change-streams/open-change-stream.php rename to source/includes/read/change-streams/change-stream.php index 1b42977d..c199e5ad 100644 --- a/source/includes/read/change-streams/open-change-stream.php +++ b/source/includes/read/change-streams/change-stream.php @@ -29,3 +29,11 @@ function toJSON(object $document): string } } 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 \ No newline at end of file diff --git a/source/read/change-streams.txt b/source/read/change-streams.txt index 35cf9f5a..a74b0814 100644 --- a/source/read/change-streams.txt +++ b/source/read/change-streams.txt @@ -38,7 +38,7 @@ database from the :atlas:`Atlas sample datasets `. To access this from your PHP application, instantiate a ``MongoDB\Client`` that connects to an Atlas cluster and assign the following value to your ``$collection`` variable: -.. literalinclude:: /includes/read/change-streams.php +.. literalinclude:: /includes/read/change/streams/change-stream.php :language: php :dedent: :start-after: start-db-coll @@ -53,7 +53,7 @@ Some examples use the ``toJSON()`` function to represent change events, which ar documents, as Extended JSON. To use this function, paste the following code into your application file: -.. literalinclude:: /includes/read/change-streams.php +.. literalinclude:: /includes/read/change-streams/change-stream.php :language: php :dedent: :start-after: start-to-json @@ -74,7 +74,7 @@ classes: The following example opens a change stream on the ``restaurants`` collection and outputs changes as they occur: -.. literalinclude:: /includes/read/change-streams.php +.. literalinclude:: /includes/read/change-streams/change-stream.php :start-after: start-open-change-stream :end-before: end-open-change-stream :language: php @@ -86,7 +86,7 @@ a document that has a ``name`` field value of ``'Blarney Castle'``: .. _php-change-stream-update: -.. literalinclude:: /includes/read/change-streams.php +.. literalinclude:: /includes/read/change-streams/change-stream.php :start-after: start-update-for-change-stream :end-before: end-update-for-change-stream :language: php @@ -123,7 +123,7 @@ The following example passes a pipeline that includes the ``$match`` stage to th ``watch()`` method. This instructs the ``watch()`` method to output events only when update operations occur: -.. literalinclude:: /includes/read/change-streams.php +.. literalinclude:: /includes/read/change-streams/change-stream-pipeline.php :start-after: start-change-stream-pipeline :end-before: end-change-stream-pipeline :language: php @@ -223,7 +223,7 @@ one of the following values: The following example calls the ``watch()`` method on a collection and includes the post-image of updated documents by setting the ``fullDocument`` option: -.. literalinclude:: /includes/read/change-streams.php +.. literalinclude:: /includes/read/change-streams/change-stream-options.php :start-after: start-change-stream-post-image :end-before: end-change-stream-post-image :language: php