File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace MongoDB \Examples ;
5
+
6
+ require '../vendor/autoload.php ' ;
7
+
8
+ use MongoDB \Client ;
9
+
10
+ use function assert ;
11
+ use function fprintf ;
12
+ use function getenv ;
13
+ use function is_object ;
14
+ use function MongoDB \BSON \fromPHP ;
15
+ use function MongoDB \BSON \toRelaxedExtendedJSON ;
16
+ use function printf ;
17
+ use function time ;
18
+
19
+ use const STDERR ;
20
+
21
+ function toJSON (object $ document ): string
22
+ {
23
+ return toRelaxedExtendedJSON (fromPHP ($ document ));
24
+ }
25
+
26
+ $ client = new Client (getenv ('MONGODB_URI ' ) ?: 'mongodb://127.0.0.1/ ' );
27
+
28
+ $ collection = $ client ->test ->coll ;
29
+ $ collection ->drop ();
30
+
31
+ $ changeStream = $ collection ->watch ();
32
+
33
+ $ documents = [];
34
+
35
+ for ($ i = 0 ; $ i < 10 ; $ i ++) {
36
+ $ documents [] = ['x ' => $ i ];
37
+ }
38
+
39
+ $ collection ->insertMany ($ documents );
40
+
41
+ $ changeStream ->rewind ();
42
+
43
+ $ startTime = time ();
44
+
45
+ while (true ) {
46
+ if ($ changeStream ->valid ()) {
47
+ $ event = $ changeStream ->current ();
48
+ assert (is_object ($ event ));
49
+ printf ("%s \n" , toJSON ($ event ));
50
+ }
51
+
52
+ $ changeStream ->next ();
53
+
54
+ if (time () - $ startTime > 3 ) {
55
+ fprintf (STDERR , "Aborting after 3 seconds... \n" );
56
+ break ;
57
+ }
58
+ }
You can’t perform that action at this time.
0 commit comments