|
1 | 1 | <?php |
2 | 2 |
|
3 | | -require __DIR__ . "/../src/QueryFlags.php"; |
4 | | -require __DIR__ . "/../src/CursorType.php"; |
5 | | -require __DIR__ . "/../src/InsertResult.php"; |
6 | | -require __DIR__ . "/../src/DeleteResult.php"; |
7 | | -require __DIR__ . "/../src/UpdateResult.php"; |
8 | | -require __DIR__ . "/../src/Collection.php"; |
9 | | - |
10 | | - |
11 | | -$manager = new MongoDB\Manager("mongodb://localhost:27017"); |
12 | | - |
13 | | - |
14 | | -$collection = new MongoDB\Collection($manager, "crud.examples"); |
15 | | -$hannes = array( |
16 | | - "name" => "Hannes", |
17 | | - "nick" => "bjori", |
18 | | - "citizen" => "Iceland", |
19 | | -); |
20 | | -$hayley = array( |
21 | | - "name" => "Hayley", |
22 | | - "nick" => "Ninja", |
23 | | - "citizen" => "USA", |
24 | | -); |
25 | | -$bobby = array( |
| 3 | +require_once __DIR__ . "/bootstrap.php"; |
| 4 | + |
| 5 | +$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); |
| 6 | +$collection = new MongoDB\Collection($manager, "phplib_demo.write"); |
| 7 | + |
| 8 | +$hannes = [ |
| 9 | + "name" => "Hannes", |
| 10 | + "nick" => "bjori", |
| 11 | + "citizen" => "Iceland", |
| 12 | +]; |
| 13 | +$hayley = [ |
| 14 | + "name" => "Hayley", |
| 15 | + "nick" => "Ninja", |
| 16 | + "citizen" => "USA", |
| 17 | +]; |
| 18 | +$bobby = [ |
26 | 19 | "name" => "Robert Fischer", |
27 | 20 | "nick" => "Bobby Fischer", |
28 | 21 | "citizen" => "USA", |
29 | | -); |
30 | | -$kasparov = array( |
| 22 | +]; |
| 23 | +$kasparov = [ |
31 | 24 | "name" => "Garry Kimovich Kasparov", |
32 | 25 | "nick" => "Kasparov", |
33 | 26 | "citizen" => "Russia", |
34 | | -); |
35 | | -$spassky = array( |
| 27 | +]; |
| 28 | +$spassky = [ |
36 | 29 | "name" => "Boris Vasilievich Spassky", |
37 | 30 | "nick" => "Spassky", |
38 | 31 | "citizen" => "France", |
39 | | -); |
| 32 | +]; |
| 33 | + |
40 | 34 |
|
41 | 35 | try { |
42 | 36 | $result = $collection->insertOne($hannes); |
43 | | - printf("Inserted _id: %s\n", $result->getInsertedId()); |
| 37 | + printf("Inserted _id: %s\n\n", $result->getInsertedId()); |
| 38 | + |
44 | 39 | $result = $collection->insertOne($hayley); |
45 | | - printf("Inserted _id: %s\n", $result->getInsertedId()); |
| 40 | + printf("Inserted _id: %s\n\n", $result->getInsertedId()); |
| 41 | + |
46 | 42 | $result = $collection->insertOne($bobby); |
47 | | - printf("Inserted _id: %s\n", $result->getInsertedId()); |
| 43 | + printf("Inserted _id: %s\n\n", $result->getInsertedId()); |
48 | 44 |
|
49 | | - $count = $collection->count(array("nick" => "bjori")); |
50 | | - printf("Searching for nick => bjori, should have only one result: %d\n", $count); |
| 45 | + $count = $collection->count(["nick" => "bjori"]); |
| 46 | + printf("Searching for nick => bjori, should have only one result: %d\n\n", $count); |
51 | 47 |
|
52 | 48 | $result = $collection->updateOne( |
53 | | - array("citizen" => "USA"), |
54 | | - array('$set' => array("citizen" => "Iceland")) |
| 49 | + ["citizen" => "USA"], |
| 50 | + ['$set' => ["citizen" => "Iceland"]] |
55 | 51 | ); |
56 | | - printf("Updated: %s (out of expected 1)\n", $result->getModifiedCount()); |
| 52 | + printf("Updated: %s (out of expected 1)\n\n", $result->getModifiedCount()); |
57 | 53 |
|
58 | | - $result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query")); |
| 54 | + $cursor = $collection->find( |
| 55 | + ["citizen" => "Iceland"], |
| 56 | + ["comment" => "Excellent query"] |
| 57 | + ); |
59 | 58 | echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n"; |
60 | | - foreach($result as $document) { |
| 59 | + foreach($cursor as $document) { |
61 | 60 | var_dump($document); |
62 | 61 | } |
| 62 | + echo "\n"; |
63 | 63 | } catch(Exception $e) { |
64 | 64 | printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); |
65 | 65 | exit; |
66 | 66 | } |
67 | 67 |
|
| 68 | + |
68 | 69 | try { |
69 | | - $result = $collection->find(); |
| 70 | + $cursor = $collection->find(); |
70 | 71 | echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n"; |
71 | | - foreach($result as $document) { |
| 72 | + foreach($cursor as $document) { |
72 | 73 | var_dump($document); |
73 | 74 | } |
| 75 | + echo "\n"; |
| 76 | + |
74 | 77 | $result = $collection->distinct("citizen"); |
75 | 78 | echo "Distinct countries:\n"; |
76 | 79 | var_dump($result); |
| 80 | + echo "\n"; |
77 | 81 |
|
78 | 82 | echo "aggregate\n"; |
79 | | - $aggregate = $collection->aggregate(array(array('$project' => array("name" => 1, "_id" => 0))), array("useCursor" => true, "batchSize" => 2)); |
| 83 | + $result = $collection->aggregate( |
| 84 | + [ |
| 85 | + ['$project' => ["name" => 1, "_id" => 0]], |
| 86 | + ], |
| 87 | + [ "useCursor" => true, "batchSize" => 2 ] |
| 88 | + ); |
80 | 89 | printf("Should be 3 different people\n"); |
81 | | - foreach($aggregate as $person) { |
| 90 | + foreach($result as $person) { |
82 | 91 | var_dump($person); |
83 | 92 | } |
84 | | - |
| 93 | + echo "\n"; |
85 | 94 | } catch(Exception $e) { |
86 | 95 | printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); |
87 | 96 | exit; |
|
90 | 99 |
|
91 | 100 | try { |
92 | 101 | $result = $collection->updateMany( |
93 | | - array("citizen" => "Iceland"), |
94 | | - array('$set' => array("viking" => true)) |
| 102 | + ["citizen" => "Iceland"], |
| 103 | + ['$set' => ["viking" => true]] |
95 | 104 | ); |
96 | | - |
97 | 105 | printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount()); |
98 | 106 | $result = $collection->find(); |
99 | 107 | foreach($result as $document) { |
100 | 108 | var_dump($document); |
101 | 109 | } |
| 110 | + echo "\n"; |
102 | 111 | } catch(Exception $e) { |
103 | 112 | printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); |
104 | 113 | exit; |
105 | 114 | } |
106 | 115 |
|
107 | 116 |
|
108 | 117 | try { |
109 | | - echo "This is the trouble maker\n"; |
110 | 118 | $result = $collection->replaceOne( |
111 | | - array("nick" => "Bobby Fischer"), |
112 | | - array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway") |
| 119 | + ["nick" => "Bobby Fischer"], |
| 120 | + ["name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway"] |
113 | 121 | ); |
114 | 122 | printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getModifiedCount()); |
115 | 123 | $result = $collection->find(); |
116 | 124 | foreach($result as $document) { |
117 | 125 | var_dump($document); |
118 | 126 | } |
| 127 | + echo "\n"; |
119 | 128 | } catch(Exception $e) { |
120 | 129 | printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); |
121 | 130 | exit; |
|
124 | 133 |
|
125 | 134 | try { |
126 | 135 | $result = $collection->deleteOne($document); |
127 | | - printf("Deleted: %d (out of expected 1)\n", $result->getDeletedCount()); |
| 136 | + printf("Deleted: %d (out of expected 1)\n\n", $result->getDeletedCount()); |
128 | 137 |
|
129 | | - $result = $collection->deleteMany(array("citizen" => "Iceland")); |
130 | | - printf("Deleted: %d (out of expected 2)\n", $result->getDeletedCount()); |
| 138 | + $result = $collection->deleteMany(["citizen" => "Iceland"]); |
| 139 | + printf("Deleted: %d (out of expected 2)\n\n", $result->getDeletedCount()); |
131 | 140 | } catch(Exception $e) { |
132 | 141 | printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); |
133 | 142 | exit; |
134 | 143 | } |
135 | 144 |
|
| 145 | + |
136 | 146 | try { |
137 | 147 | echo "FindOneAndReplace\n"; |
138 | | - $result = $collection->findOneAndReplace($spassky, $kasparov, array("upsert" => true)); |
| 148 | + $result = $collection->findOneAndReplace( |
| 149 | + $spassky, |
| 150 | + $kasparov, |
| 151 | + ["upsert" => true] |
| 152 | + ); |
139 | 153 | echo "Kasparov\n"; |
140 | 154 | var_dump($result); |
| 155 | + echo "\n"; |
141 | 156 |
|
142 | 157 | echo "Returning the old document where he was Russian\n"; |
143 | | - $result = $collection->findOneAndUpdate($kasparov, array('$set' => array("citizen" => "Croatia"))); |
| 158 | + $result = $collection->findOneAndUpdate( |
| 159 | + $kasparov, |
| 160 | + ['$set' => ["citizen" => "Croatia"]] |
| 161 | + ); |
144 | 162 | var_dump($result); |
| 163 | + echo "\n"; |
145 | 164 |
|
146 | 165 | echo "Deleting him, he isn't Croatian just yet\n"; |
147 | | - $result = $collection->findOneAndDelete(array("citizen" => "Croatia")); |
| 166 | + $result = $collection->findOneAndDelete(["citizen" => "Croatia"]); |
148 | 167 | var_dump($result); |
| 168 | + echo "\n"; |
149 | 169 |
|
150 | 170 | echo "This should be empty\n"; |
151 | | - $result = $collection->find(array()); |
| 171 | + $result = $collection->find(); |
152 | 172 | foreach($result as $document) { |
153 | 173 | var_dump($document); |
154 | 174 | } |
| 175 | + echo "\n"; |
155 | 176 | } catch(Exception $e) { |
156 | 177 | printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); |
157 | 178 | exit; |
|
162 | 183 | $result = $collection->bulkWrite( |
163 | 184 | // Required writes param (an array of operations) |
164 | 185 | [ |
165 | | - // Like explain(), operations identified by single key |
| 186 | + // Operations identified by single key |
166 | 187 | [ |
167 | 188 | 'insertOne' => [ |
168 | 189 | ['x' => 1] |
|
204 | 225 | printf("deletedCount: %d\n", $result->getDeletedCount()); |
205 | 226 |
|
206 | 227 | foreach ($result->getUpsertedIds() as $index => $id) { |
207 | | - printf("upsertedId[%d]: %s", $index, $id); |
| 228 | + printf("upsertedId[%d]: %s\n", $index, $id); |
208 | 229 | } |
209 | 230 |
|
210 | 231 | } catch(Exception $e) { |
211 | 232 | printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); |
212 | | - echo $e->getTraceAsString(), "\n"; |
213 | 233 | exit; |
214 | 234 | } |
0 commit comments