Skip to content

Commit 13ded9a

Browse files
committed
CRUD API examples
1 parent b646280 commit 13ded9a

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

examples/find.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88

99
var_dump($manager);
10-
$collection = new MongoDB\Collection($manager, "phongo_test.functional_cursor_001");
11-
$result = $collection->find(array("username" => "pacocha.quentin"), array("projection" => array("firstName" =>1)));
10+
$collection = new MongoDB\Collection($manager, "crud.examples");
11+
$result = $collection->find(array("nick" => "bjori"), array("projection" => array("name" =>1)));
1212

1313

1414
foreach($result as $document) {

examples/write.php

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,79 @@
1414
);
1515
$hayley = array(
1616
"name" => "Hayley",
17-
"nick" => "Alien Ninja",
17+
"nick" => "Ninja",
1818
"citizen" => "USA",
1919
);
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",
2424
);
2525

2626
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+
2856
} catch(Exception $e) {
2957
echo $e->getMessage(), "\n";
3058
exit;
3159
}
3260

3361
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());
3587
} catch(Exception $e) {
3688
echo $e->getMessage(), "\n";
3789
exit;
38-
}
39-
4090

91+
}
4192

0 commit comments

Comments
 (0)