File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-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 fprintf ;
11
+ use function getenv ;
12
+ use function hrtime ;
13
+ use function MongoDB \BSON \fromPHP ;
14
+ use function MongoDB \BSON \toRelaxedExtendedJSON ;
15
+ use function printf ;
16
+
17
+ use const STDERR ;
18
+
19
+ function toJSON (object $ document ): string
20
+ {
21
+ return toRelaxedExtendedJSON (fromPHP ($ document ));
22
+ }
23
+
24
+ $ client = new Client (getenv ('MONGODB_URI ' ) ?? null );
25
+
26
+ $ collection = $ client ->test ->coll ;
27
+ $ collection ->drop ();
28
+
29
+ $ changeStream = $ collection ->watch ();
30
+
31
+ $ documents = [];
32
+
33
+ for ($ i = 0 ; $ i < 10 ; $ i ++) {
34
+ $ documents [] = ['x ' => $ i ];
35
+ }
36
+
37
+ $ collection ->insertMany ($ documents );
38
+
39
+ $ changeStream ->rewind ();
40
+
41
+ $ startTime = hrtime (true );
42
+
43
+ while (true ) {
44
+ if ($ changeStream ->valid ()) {
45
+ printf ("%s \n" , toJSON ($ changeStream ->current ()));
46
+ }
47
+
48
+ $ changeStream ->next ();
49
+
50
+ if (hrtime (true ) - $ startTime > 5e+9 ) {
51
+ fprintf (STDERR , "Aborting after 5 seconds... \n" );
52
+ break ;
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments