@@ -18,18 +18,16 @@ function toJSON(object $document): string
18
18
// Monitors and prints changes to the "restaurants" collection
19
19
// start-open-change-stream
20
20
$ changeStream = $ collection ->watch ();
21
+ $ changeStream ->rewind ();
21
22
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 ();
28
25
29
- if ($ event ['operationType ' ] === 'invalidate ' ) {
30
- break ;
26
+ if ($ changeStream ->valid ()) {
27
+ $ event = $ changeStream ->current ();
28
+ echo toJSON ($ event ), PHP_EOL ;
31
29
}
32
- }
30
+ } while { $ event [ ' operationType ' ] !== ' invalidate ' };
33
31
// end-open-change-stream
34
32
35
33
// Updates a document that has a "name" value of "Blarney Castle"
@@ -44,35 +42,32 @@ function toJSON(object $document): string
44
42
// start-change-stream-pipeline
45
43
$ pipeline = [['$match ' => ['operationType ' => 'update ' ]]];
46
44
$ changeStream = $ collection ->watch ($ pipeline );
45
+ $ changeStream ->rewind ();
47
46
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 ();
54
49
55
- if ($ event ['operationType ' ] === 'invalidate ' ) {
56
- break ;
50
+ if ($ changeStream ->valid ()) {
51
+ $ event = $ changeStream ->current ();
52
+ echo toJSON ($ event ), PHP_EOL ;
57
53
}
58
- }
54
+
55
+ } while ($ event ['operationType ' ] !== 'invalidate ' );
59
56
// end-change-stream-pipeline
60
57
61
58
// Passes an options argument to watch() to include the post-image of updated documents
62
59
// start-change-stream-post-image
63
60
$ options = ['fullDocument ' => MongoDB \Operation \Watch::FULL_DOCUMENT_UPDATE_LOOKUP ];
64
61
$ changeStream = $ collection ->watch ([], $ options );
62
+ $ changeStream ->rewind ();
65
63
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 ();
72
66
73
- if ($ event ['operationType ' ] === 'invalidate ' ) {
74
- break ;
67
+ if ($ changeStream ->valid ()) {
68
+ $ event = $ changeStream ->current ();
69
+ echo toJSON ($ event ), PHP_EOL ;
75
70
}
76
- }
71
+ } while ( $ event [ ' operationType ' ] !== ' invalidate ' );
77
72
// end-change-stream-post-image
78
73
0 commit comments