Commit 13ded9a3 authored by Hannes Magnusson's avatar Hannes Magnusson

CRUD API examples

parent b646280f
...@@ -7,8 +7,8 @@ $manager = new MongoDB\Manager("mongodb://localhost:27017"); ...@@ -7,8 +7,8 @@ $manager = new MongoDB\Manager("mongodb://localhost:27017");
var_dump($manager); var_dump($manager);
$collection = new MongoDB\Collection($manager, "phongo_test.functional_cursor_001"); $collection = new MongoDB\Collection($manager, "crud.examples");
$result = $collection->find(array("username" => "pacocha.quentin"), array("projection" => array("firstName" =>1))); $result = $collection->find(array("nick" => "bjori"), array("projection" => array("name" =>1)));
foreach($result as $document) { foreach($result as $document) {
......
...@@ -14,28 +14,79 @@ $hannes = array( ...@@ -14,28 +14,79 @@ $hannes = array(
); );
$hayley = array( $hayley = array(
"name" => "Hayley", "name" => "Hayley",
"nick" => "Alien Ninja", "nick" => "Ninja",
"citizen" => "USA", "citizen" => "USA",
); );
$jonpall = array( $bobby = array(
"name" => "Jon Pall", "name" => "Robert Fischer",
"nick" => "unknown", "nick" => "Bobby Fischer",
"citizen" => "Iceland", "citizen" => "USA",
); );
try { try {
$hannes_id = $collection->insertOne($hannes); $result = $collection->insertOne($hannes);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->insertOne($hayley);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->insertOne($bobby);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->find(array("nick" => "bjori"), array("projection" => array("name" => 1)));
echo "Searching for nick => bjori, should have only one result:\n";
foreach($result as $document) {
var_dump($document);
}
$result = $collection->deleteOne($document);
printf("Deleted: %s (out of expected 1)\n", $result->getNumRemoved());
$result = $collection->updateOne(
array("citizen" => "USA"),
array('$set' => array("citizen" => "Iceland"))
);
printf("Updated: %s (out of expected 1)\n", $result->getNumModified());
$result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query"));
echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n";
foreach($result as $document) {
var_dump($document);
}
$result = $collection->deleteOne($document);
printf("Deleted: %d (out of expected 1)\n", $result->getNumRemoved());
} catch(Exception $e) { } catch(Exception $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
exit; exit;
} }
try { try {
$results = $collection->insertMany(array($hayley, $jonpall)); /* These two were removed earlier */
$result = $collection->insertOne($hannes);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->insertOne($hayley);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->find();
echo "Find all docs, should be 3, verify 2x USA citizen, 1 Icelandic\n";
foreach($result as $document) {
var_dump($document);
}
$result = $collection->updateMany(
array("citizen" => "USA"),
array('$set' => array("citizen" => "Iceland"))
);
printf("Updated: %d (out of expected 2), verify everyone is Icelandic\n", $result->getNumModified());
$result = $collection->find();
foreach($result as $document) {
var_dump($document);
}
$result = $collection->deleteMany(array("citizen" => "Iceland"));
printf("Deleted: %d (out of expected 3)\n", $result->getNumRemoved());
} catch(Exception $e) { } catch(Exception $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
exit; exit;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment