-
Notifications
You must be signed in to change notification settings - Fork 208
Split and modify CRUD examples #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
$ordered = true; | ||
|
||
$insertBatch = new \MongoDB\Batch\InsertBatch($ordered); | ||
$insertBatch | ||
->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() | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
class Person implements BsonSerializable { | ||
protected $name; | ||
protected $nick; | ||
protected $addresses = array(); | ||
|
||
function __construct($name, $nick, array $addresses) { | ||
$this->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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
// 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($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() | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed Mongolizable to BsonSerializable. This mimics JsonSerializable and I don't see the harm in referring to BSON if we're already going to do it in the type mappings elsewhere.