|
14 | 14 | );
|
15 | 15 | $hayley = array(
|
16 | 16 | "name" => "Hayley",
|
17 |
| - "nick" => "Alien Ninja", |
| 17 | + "nick" => "Ninja", |
18 | 18 | "citizen" => "USA",
|
19 | 19 | );
|
20 |
| -$jonpall = array( |
21 |
| - "name" => "Jon Pall", |
22 |
| - "nick" => "unknown", |
23 |
| - "citizen" => "Iceland", |
| 20 | +$bobby = array( |
| 21 | + "name" => "Robert Fischer", |
| 22 | + "nick" => "Bobby Fischer", |
| 23 | + "citizen" => "USA", |
24 | 24 | );
|
25 | 25 |
|
26 | 26 | try {
|
27 |
| - $hannes_id = $collection->insertOne($hannes); |
| 27 | + $result = $collection->insertOne($hannes); |
| 28 | + printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); |
| 29 | + $result = $collection->insertOne($hayley); |
| 30 | + printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); |
| 31 | + $result = $collection->insertOne($bobby); |
| 32 | + printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); |
| 33 | + |
| 34 | + $result = $collection->find(array("nick" => "bjori"), array("projection" => array("name" => 1))); |
| 35 | + echo "Searching for nick => bjori, should have only one result:\n"; |
| 36 | + foreach($result as $document) { |
| 37 | + var_dump($document); |
| 38 | + } |
| 39 | + |
| 40 | + $result = $collection->deleteOne($document); |
| 41 | + printf("Deleted: %s (out of expected 1)\n", $result->getNumRemoved()); |
| 42 | + $result = $collection->updateOne( |
| 43 | + array("citizen" => "USA"), |
| 44 | + array('$set' => array("citizen" => "Iceland")) |
| 45 | + ); |
| 46 | + printf("Updated: %s (out of expected 1)\n", $result->getNumModified()); |
| 47 | + |
| 48 | + $result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query")); |
| 49 | + echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n"; |
| 50 | + foreach($result as $document) { |
| 51 | + var_dump($document); |
| 52 | + } |
| 53 | + $result = $collection->deleteOne($document); |
| 54 | + printf("Deleted: %d (out of expected 1)\n", $result->getNumRemoved()); |
| 55 | + |
28 | 56 | } catch(Exception $e) {
|
29 | 57 | echo $e->getMessage(), "\n";
|
30 | 58 | exit;
|
31 | 59 | }
|
32 | 60 |
|
33 | 61 | try {
|
34 |
| - $results = $collection->insertMany(array($hayley, $jonpall)); |
| 62 | + /* These two were removed earlier */ |
| 63 | + $result = $collection->insertOne($hannes); |
| 64 | + printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); |
| 65 | + $result = $collection->insertOne($hayley); |
| 66 | + printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted()); |
| 67 | + |
| 68 | + $result = $collection->find(); |
| 69 | + echo "Find all docs, should be 3, verify 2x USA citizen, 1 Icelandic\n"; |
| 70 | + foreach($result as $document) { |
| 71 | + var_dump($document); |
| 72 | + } |
| 73 | + |
| 74 | + $result = $collection->updateMany( |
| 75 | + array("citizen" => "USA"), |
| 76 | + array('$set' => array("citizen" => "Iceland")) |
| 77 | + ); |
| 78 | + |
| 79 | + printf("Updated: %d (out of expected 2), verify everyone is Icelandic\n", $result->getNumModified()); |
| 80 | + $result = $collection->find(); |
| 81 | + foreach($result as $document) { |
| 82 | + var_dump($document); |
| 83 | + } |
| 84 | + |
| 85 | + $result = $collection->deleteMany(array("citizen" => "Iceland")); |
| 86 | + printf("Deleted: %d (out of expected 3)\n", $result->getNumRemoved()); |
35 | 87 | } catch(Exception $e) {
|
36 | 88 | echo $e->getMessage(), "\n";
|
37 | 89 | exit;
|
38 |
| -} |
39 |
| - |
40 | 90 |
|
| 91 | +} |
41 | 92 |
|
0 commit comments