@@ -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
0 commit comments