1
+ <?php
2
+
3
+ require 'vendor/autoload.php ' ;
4
+
5
+ use MongoDB \Client ;
6
+
7
+
8
+ // start-setup
9
+ $ uri = "<connection string> " ;
10
+ $ client = new Client ($ uri );
11
+
12
+ $ db = $ client ->db ;
13
+ $ collection = $ db ->fruits ;
14
+
15
+ // Inserts documents representing fruits
16
+ $ fruits = [
17
+ [
18
+ '_id ' => 1 ,
19
+ 'name ' => 'apples ' ,
20
+ 'qty ' => 5 ,
21
+ 'rating ' => 3 ,
22
+ 'color ' => 'red ' ,
23
+ 'type ' => ['fuji ' , 'honeycrisp ' ]
24
+ ],
25
+ [
26
+ '_id ' => 2 ,
27
+ 'name ' => 'bananas ' ,
28
+ 'qty ' => 7 ,
29
+ 'rating ' => 4 ,
30
+ 'color ' => 'yellow ' ,
31
+ 'type ' => ['cavendish ' ]
32
+ ],
33
+ [
34
+ '_id ' => 3 ,
35
+ 'name ' => 'oranges ' ,
36
+ 'qty ' => 6 ,
37
+ 'rating ' => 2 ,
38
+ 'type ' => ['naval ' , 'mandarin ' ]
39
+ ],
40
+ [
41
+ '_id ' => 4 ,
42
+ 'name ' => 'pineapples ' ,
43
+ 'qty ' => 3 ,
44
+ 'rating ' => 5 ,
45
+ 'color ' => 'yellow '
46
+ ]
47
+ ];
48
+
49
+ $ result = $ collection ->insertMany ($ fruits );
50
+ // end-setup
51
+
52
+ // Retrieves documents in which the "color" value is "yellow"
53
+ // start-find-exact
54
+ $ cursor = $ collection ->find (['color ' => 'yellow ' ]);
55
+ foreach ($ cursor as $ doc ) {
56
+ echo json_encode ($ doc ) . PHP_EOL ;
57
+ }
58
+ // end-find-exact
59
+
60
+ // Retrieves all documents in the collection
61
+ // start-find-all
62
+ $ cursor = $ collection ->find ([]);
63
+ foreach ($ cursor as $ doc ) {
64
+ echo json_encode ($ doc ) . PHP_EOL ;
65
+ }
66
+ // end-find-all
67
+
68
+ // Retrieves and prints documents in which the "rating" value is greater than 2
69
+ // start-find-comparison
70
+ $ cursor = $ collection ->find (['rating ' => ['$gt ' => 2 ]]);
71
+ foreach ($ cursor as $ doc ) {
72
+ echo json_encode ($ doc ) . PHP_EOL ;
73
+ }
74
+ // end-find-comparison
75
+
76
+ // Retrieves and prints documents that match one or both query filters
77
+ // start-find-logical
78
+ $ cursor = $ collection ->find ([
79
+ '$or ' => [
80
+ ['qty ' => ['$gt ' => 5 ]],
81
+ ['color ' => 'yellow ' ]
82
+ ]
83
+ ]);
84
+ foreach ($ cursor as $ doc ) {
85
+ echo json_encode ($ doc ) . PHP_EOL ;
86
+ }
87
+ // end-find-logical
88
+
89
+ // Retrieves and prints documents in which the "type" array has 2 elements
90
+ // start-find-array
91
+ $ cursor = $ collection ->find (['type ' => ['$size ' => 2 ]]);
92
+ foreach ($ cursor as $ doc ) {
93
+ echo json_encode ($ doc ) . PHP_EOL ;
94
+ }
95
+ // end-find-array
96
+
97
+ // Retrieves and prints documents that have a "color" field
98
+ // start-find-element
99
+ $ cursor = $ collection ->find (['color ' => ['$exists ' => true ]]);
100
+ foreach ($ cursor as $ doc ) {
101
+ echo json_encode ($ doc ) . PHP_EOL ;
102
+ }
103
+ // end-find-element
104
+
105
+ // Retrieves and prints documents in which the "name" value has at least two consecutive "p" characters
106
+ // start-find-evaluation
107
+ $ cursor = $ collection ->find (['name ' => ['$regex ' => 'p{2,} ' ]]);
108
+ foreach ($ cursor as $ doc ) {
109
+ echo json_encode ($ doc ) . PHP_EOL ;
110
+ }
111
+ // end-find-evaluation
112
+
113
+ ?>
0 commit comments