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..3e1d25c07 --- /dev/null +++ b/docs/examples/insert.php @@ -0,0 +1,26 @@ +add($hannes) + ->add($hayley); + +$w = 'majority'; +$w = 500; + +$writeConcern = new \MongoDB\WriteConcern($w, $wtimeout); + +$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR"); + +$result = $mm->executeWrite("db.collection", $insertBatch, $writeConcern); + +assert($result instanceof \MongoDB\WriteResult); + +printf( + "Inserted %d documents %s in %d msec\n", + $result->getOpCount(), + $result->getServer()->getHostname(), + $result->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..87330d1f9 --- /dev/null +++ b/docs/examples/query.php @@ -0,0 +1,39 @@ + array("name" => "Hannes"), + '$orderBy' => array('name' => 1), + '$comment' => 'More special stuff', +); + +$query = new \MongoDB\Query($queryObj); +$query + ->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); + +$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"); + +$result = $mm->executeQuery("db.collection", $query, $readPreference); + +assert($result instanceof \MongoDB\QueryResult); +assert($result instanceof \IteratorAggregate); +assert($result->getIterator() instanceof \MongoDB\Cursor); + +foreach($result as $person) { + printf( + "%s has lived in %s\n", + $person->getName(), + $person->getAddresses()[0]->getCountry()->getName() + ); +}