Skip to content

Commit 4129a38

Browse files
authored
DOCSP-43912: Improve change stream code examples (#264)
1 parent 20c0241 commit 4129a38

File tree

6 files changed

+142
-87
lines changed

6 files changed

+142
-87
lines changed

source/includes/read/change-streams.php

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
while (true) {
25+
$changeStream->next();
26+
27+
if ($changeStream->valid()) {
28+
continue;
29+
}
30+
31+
$event = $changeStream->current();
32+
echo toJSON($event), PHP_EOL;
33+
34+
if ($changeStream->current()['operationType'] === 'invalidate') {
35+
break;
36+
}
37+
}
38+
// end-change-stream-post-image
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// Updates a document that has a "name" value of "Blarney Castle"
19+
// start-update-for-change-stream
20+
$result = $collection->updateOne(
21+
['name' => 'Blarney Castle'],
22+
['$set' => ['cuisine' => 'Irish']]
23+
);
24+
// end-update-for-change-stream
25+
26+
// Passes a pipeline argument to watch() to monitor only update operations
27+
// start-change-stream-pipeline
28+
$pipeline = [['$match' => ['operationType' => 'update']]];
29+
$changeStream = $collection->watch($pipeline);
30+
$changeStream->rewind();
31+
32+
while (true) {
33+
$changeStream->next();
34+
35+
if ($changeStream->valid()) {
36+
continue;
37+
}
38+
39+
$event = $changeStream->current();
40+
echo toJSON($event), PHP_EOL;
41+
42+
if ($changeStream->current()['operationType'] === 'invalidate') {
43+
break;
44+
}
45+
}
46+
// end-change-stream-pipeline
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
while (true) {
24+
$changeStream->next();
25+
26+
if ($changeStream->valid()) {
27+
continue;
28+
}
29+
30+
$event = $changeStream->current();
31+
echo toJSON($event), PHP_EOL;
32+
33+
if ($changeStream->current()['operationType'] === 'invalidate') {
34+
break;
35+
}
36+
}
37+
// end-open-change-stream
38+
39+
// Updates a document that has a "name" value of "Blarney Castle"
40+
// start-update-for-change-stream
41+
$result = $collection->updateOne(
42+
['name' => 'Blarney Castle'],
43+
['$set' => ['cuisine' => 'Irish']]
44+
);
45+
// end-update-for-change-stream

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,19 @@
5252
// Data Changes
5353
// start-change-stream
5454
$changeStream = $collection->watch();
55+
$changeStream->rewind();
5556

56-
for ($changeStream->rewind(); true; $changeStream->next()) {
57-
if ( ! $changeStream->valid()) {
57+
while (true) {
58+
$changeStream->next();
59+
60+
if ($changeStream->valid()) {
5861
continue;
5962
}
63+
6064
$event = $changeStream->current();
6165
echo toJSON($event), PHP_EOL;
6266

63-
if ($event['operationType'] === 'invalidate') {
67+
if ($changeStream->current()['operationType'] === 'invalidate') {
6468
break;
6569
}
6670
}

source/read/change-streams.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ database from the :atlas:`Atlas sample datasets </sample-data>`. To access this
3838
from your PHP application, instantiate a ``MongoDB\Client`` that connects to an Atlas cluster
3939
and assign the following value to your ``$collection`` variable:
4040

41-
.. literalinclude:: /includes/read/change-streams.php
41+
.. literalinclude:: /includes/read/change/streams/change-stream.php
4242
:language: php
4343
:dedent:
4444
:start-after: start-db-coll
@@ -53,7 +53,7 @@ Some examples use the ``toJSON()`` function to represent change events, which ar
5353
documents, as Extended JSON. To use this function, paste the following code into your
5454
application file:
5555

56-
.. literalinclude:: /includes/read/change-streams.php
56+
.. literalinclude:: /includes/read/change-streams/change-stream.php
5757
:language: php
5858
:dedent:
5959
:start-after: start-to-json
@@ -74,7 +74,7 @@ classes:
7474
The following example opens a change stream on the ``restaurants`` collection
7575
and outputs changes as they occur:
7676

77-
.. literalinclude:: /includes/read/change-streams.php
77+
.. literalinclude:: /includes/read/change-streams/change-stream.php
7878
:start-after: start-open-change-stream
7979
:end-before: end-open-change-stream
8080
:language: php
@@ -86,7 +86,7 @@ a document that has a ``name`` field value of ``'Blarney Castle'``:
8686

8787
.. _php-change-stream-update:
8888

89-
.. literalinclude:: /includes/read/change-streams.php
89+
.. literalinclude:: /includes/read/change-streams/change-stream.php
9090
:start-after: start-update-for-change-stream
9191
:end-before: end-update-for-change-stream
9292
:language: php
@@ -123,7 +123,7 @@ The following example passes a pipeline that includes the ``$match`` stage to th
123123
``watch()`` method. This instructs the ``watch()`` method to output events only
124124
when update operations occur:
125125

126-
.. literalinclude:: /includes/read/change-streams.php
126+
.. literalinclude:: /includes/read/change-streams/change-stream-pipeline.php
127127
:start-after: start-change-stream-pipeline
128128
:end-before: end-change-stream-pipeline
129129
:language: php
@@ -223,7 +223,7 @@ one of the following values:
223223
The following example calls the ``watch()`` method on a collection and includes the post-image
224224
of updated documents by setting the ``fullDocument`` option:
225225

226-
.. literalinclude:: /includes/read/change-streams.php
226+
.. literalinclude:: /includes/read/change-streams/change-stream-options.php
227227
:start-after: start-change-stream-post-image
228228
:end-before: end-change-stream-post-image
229229
:language: php

0 commit comments

Comments
 (0)