From 594dbdbac1d5b7ebba686d52d763a53d0a36446e Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 13 May 2014 13:44:02 -0400 Subject: [PATCH 1/3] Split and modify CRUD examples --- docs/examples/crud.php | 136 --------------------------------------- docs/examples/insert.php | 27 ++++++++ docs/examples/model.php | 62 ++++++++++++++++++ docs/examples/query.php | 41 ++++++++++++ 4 files changed, 130 insertions(+), 136 deletions(-) delete mode 100644 docs/examples/crud.php create mode 100644 docs/examples/insert.php create mode 100644 docs/examples/model.php create mode 100644 docs/examples/query.php diff --git a/docs/examples/crud.php b/docs/examples/crud.php deleted file mode 100644 index dd498ef14..000000000 --- a/docs/examples/crud.php +++ /dev/null @@ -1,136 +0,0 @@ -name = $name; - $this->nick = $nick; - foreach($addresses as $address) { - if ($address instanceof Address) { - $this->addresses[] = $address; - } - } - } - - function getAddresses() { - return $this->addresses; - } -} -class Address implements Mongolizable { - protected $address; - protected $country; - - function __construct($address, Country $country) { - $this->address = $address; - $this->country = $country; - } - function getCountry() { - return $this->country; - } -} - -class Country implements Mongolizable { - protected $name; - - function __construct($name) { - $this->name; - } - - function getName() { - return $name; - } -} - - -$norway = new Country("Norway"); -$iceland = new Country("Iceland"); -$usa = new Country("USA"); - -$menlo = new Address("22 Coleman Pl", $usa); - -$addresses = array( - new Address("Dynekilgata 15", $norway), - new Address("Mánabraut 4", $iceland), - $menlo, -); - -$hannes = new Person("Hannes", "bjori", $addresses); -$hayley = new Person("Hayley", "Ninja", array($menlo)); - - - - -/* Read/Write general options */ -$writeOptions = array( - "ordered" => true, - "writeConcern" => array( - "w" => 3, - "wtimeout" => 42, - ), -); -$readPreference = array( - "type" => \MongoDB\ReadPreference\SECONDARY_PREFERRED, - "tags" => array( - array("dc:eu", "cpu:fast"), - array("dc:eu", "cc:is"), - array(""), - ), -); - - - - -/* Constructing a query */ -$query = array( - '$query' => array("name" => "Hannes"), - '$orderBy' => array('name' => 1), - '$comment' => 'More special stuff', - '$select' => array("fieldname" => 1, "fieldname.two" => 1), /* Internally rewritten to wire protocol option */ - '$skip' => 0, /* Don't skip any docs */ /* Internally rewritten to wire protocol option */ - '$limit' => -3, /* Kill server cursor after 3 docs */ /* Internally rewritten to wire protocol option */ - '$flags' => EXHAUST | NO_CURSOR_TIMEOUT, /* Internally rewritten to wire protocol option */ -); - -$queryHannes = new Query($query); -$queryUSA = new Query(array('$query' => array('addresses.$.Country' => "USA"))); - - - - -/* Main object. Manager instance / MongoClient */ -$mm = new MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR"); - - - -$batch = new MongoDB\Batch\Write; -$batch->add($hannes)->add($hayley); - -$results = $mm->executeWrite("myproject.people", $batch, $writeOptions); -$results instanceof WriteResults; - -echo "Wrote ", $results->getOpCount(), " to ", $results->getServer()->getHostname(), " in ", $results->getTimer(), " ms\n"; - - - -$results = $mm->executeQuery("myproject.people", $queryHannes, $readPreference); -$results instanceof QueryResults; - - - -foreach($results as $person) { - echo $person->getName() , " has lived in ", $person->getAddresses()[0]->getCountry()->getName(), "\n"; -} - - - - - - - - - - - - diff --git a/docs/examples/insert.php b/docs/examples/insert.php new file mode 100644 index 000000000..952c1595d --- /dev/null +++ b/docs/examples/insert.php @@ -0,0 +1,27 @@ +add($hannes) + ->add($hayley); + +$writeOptions = array( + "ordered" => true, + "writeConcern" => array( + "w" => 3, + "wtimeout" => 42, + ), +); + +$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR"); + +$results = $mm->executeWrite("db.collection", $batch, $writeOptions); + +assert($results instanceof \MongoDB\WriteResults); + +printf( + "Inserted %d documents %s in %d msec\n", + $results->getOpCount(), + $results->getServer()->getHostname(), + $results->getTimer() +); diff --git a/docs/examples/model.php b/docs/examples/model.php new file mode 100644 index 000000000..2a89a0be2 --- /dev/null +++ b/docs/examples/model.php @@ -0,0 +1,62 @@ +name = $name; + $this->nick = $nick; + foreach($addresses as $address) { + if ($address instanceof Address) { + $this->addresses[] = $address; + } + } + } + + function getAddresses() { + return $this->addresses; + } +} + +class Address implements BsonSerializable { + protected $address; + protected $country; + + function __construct($address, Country $country) { + $this->address = $address; + $this->country = $country; + } + + function getCountry() { + return $this->country; + } +} + +class Country implements BsonSerializable { + protected $name; + + function __construct($name) { + $this->name; + } + + function getName() { + return $name; + } +} + +$norway = new Country("Norway"); +$iceland = new Country("Iceland"); +$usa = new Country("USA"); + +$menlo = new Address("22 Coleman Pl", $usa); + +$addresses = array( + new Address("Dynekilgata 15", $norway), + new Address("Mánabraut 4", $iceland), + $menlo, +); + +$hannes = new Person("Hannes", "bjori", $addresses); +$hayley = new Person("Hayley", "Ninja", array($menlo)); diff --git a/docs/examples/query.php b/docs/examples/query.php new file mode 100644 index 000000000..d46a118a8 --- /dev/null +++ b/docs/examples/query.php @@ -0,0 +1,41 @@ + array("name" => "Hannes"), + '$orderBy' => array('name' => 1), + '$comment' => 'More special stuff', +); + +$queryFields = array( + 'fieldname' => 1, + 'fieldname.two' => 1, +); + +$flags = EXHAUST | NO_CURSOR_TIMEOUT; +$limit = -3; // Kill server cursor after 3 documents +$skip = 0; // Don't skip any documents + +$query = new \MongoDB\Query($queryDocument, $queryFields, $flags, $limit, $skip); + +$readPreference = array( + "type" => 'secondaryPreferred', + "tags" => array( + array("dc:eu", "cpu:fast"), + array("dc:eu", "cc:is"), + array(""), + ), +); + +$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR"); + +$cursor = $mm->executeQuery("db.collection", $query, $readPreference); + +assert($cursor instanceof \MongoDB\Cursor); + +foreach($cursor as $person) { + printf( + "%s has lived in %s\n", + $person->getName(), + $person->getAddresses()[0]->getCountry()->getName() + ); +} From 8a00ff2ef593bd3a604894260c4557c4bb5ab837 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 13 May 2014 17:11:33 -0400 Subject: [PATCH 2/3] Use value objects and make QueryResult IteratorAggregate --- docs/examples/insert.php | 27 +++++++++++------------ docs/examples/query.php | 47 ++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/docs/examples/insert.php b/docs/examples/insert.php index 952c1595d..3e1d25c07 100644 --- a/docs/examples/insert.php +++ b/docs/examples/insert.php @@ -1,27 +1,26 @@ add($hannes) ->add($hayley); -$writeOptions = array( - "ordered" => true, - "writeConcern" => array( - "w" => 3, - "wtimeout" => 42, - ), -); +$w = 'majority'; +$w = 500; + +$writeConcern = new \MongoDB\WriteConcern($w, $wtimeout); $mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR"); -$results = $mm->executeWrite("db.collection", $batch, $writeOptions); +$result = $mm->executeWrite("db.collection", $insertBatch, $writeConcern); -assert($results instanceof \MongoDB\WriteResults); +assert($result instanceof \MongoDB\WriteResult); printf( "Inserted %d documents %s in %d msec\n", - $results->getOpCount(), - $results->getServer()->getHostname(), - $results->getTimer() + $result->getOpCount(), + $result->getServer()->getHostname(), + $result->getTimer() ); diff --git a/docs/examples/query.php b/docs/examples/query.php index d46a118a8..6eeccef31 100644 --- a/docs/examples/query.php +++ b/docs/examples/query.php @@ -1,38 +1,33 @@ array("name" => "Hannes"), - '$orderBy' => array('name' => 1), - '$comment' => 'More special stuff', +$criteria = array("name" => "Hannes"); +$selector = array('fieldname' => 1, 'fieldname.two' => 1); + +$query = new \MongoDB\Query($criteria, $selector); +$query + ->setOrderBy(array('name' => 1)) + ->setComment('More special stuff') + ->setLimit(-3) // Kill server cursor after 3 documents + ->setSkip(0) // Don't skip any documents + ->setFlags(EXHAUST | NO_CURSOR_TIMEOUT); + +$tags = array( + array("dc:eu", "cpu:fast"), + array("dc:eu", "cc:is"), + array(""), ); -$queryFields = array( - 'fieldname' => 1, - 'fieldname.two' => 1, -); - -$flags = EXHAUST | NO_CURSOR_TIMEOUT; -$limit = -3; // Kill server cursor after 3 documents -$skip = 0; // Don't skip any documents - -$query = new \MongoDB\Query($queryDocument, $queryFields, $flags, $limit, $skip); - -$readPreference = array( - "type" => 'secondaryPreferred', - "tags" => array( - array("dc:eu", "cpu:fast"), - array("dc:eu", "cc:is"), - array(""), - ), -); +$readPreference = new \MongoDB\ReadPreference('secondaryPreferred', $tags); $mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR"); -$cursor = $mm->executeQuery("db.collection", $query, $readPreference); +$result = $mm->executeQuery("db.collection", $query, $readPreference); -assert($cursor instanceof \MongoDB\Cursor); +assert($result instanceof \MongoDB\QueryResult); +assert($result instanceof \IteratorAggregate); +assert($result->getIterator() instanceof \MongoDB\Cursor); -foreach($cursor as $person) { +foreach($result as $person) { printf( "%s has lived in %s\n", $person->getName(), From eee840c881a1eaea14e50e90ff40dcbec8fe7bf4 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 13 May 2014 17:28:20 -0400 Subject: [PATCH 3/3] Query constructor should take query object, not just criteria --- docs/examples/query.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/examples/query.php b/docs/examples/query.php index 6eeccef31..87330d1f9 100644 --- a/docs/examples/query.php +++ b/docs/examples/query.php @@ -1,12 +1,15 @@ "Hannes"); -$selector = array('fieldname' => 1, 'fieldname.two' => 1); +// Corresponds to "query object" in http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query +$queryObj = array( + '$query' => array("name" => "Hannes"), + '$orderBy' => array('name' => 1), + '$comment' => 'More special stuff', +); -$query = new \MongoDB\Query($criteria, $selector); +$query = new \MongoDB\Query($queryObj); $query - ->setOrderBy(array('name' => 1)) - ->setComment('More special stuff') + ->setSelector(array('fieldname' => 1, 'fieldname.two' => 1)) ->setLimit(-3) // Kill server cursor after 3 documents ->setSkip(0) // Don't skip any documents ->setFlags(EXHAUST | NO_CURSOR_TIMEOUT);