-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-987: Add more examples #981
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
12 commits
Select commit
Hold shift + click to select a range
a32da14
Fix include path for autoload file
alcaeus 498da74
Add examples for bulk writes and with_transaction
alcaeus 17ccb2f
Add aggregation example
alcaeus eee1ab3
Add example for persistable objects
alcaeus b47a93e
Ignore certain phpcs errors for all examples
alcaeus 9aeb3b0
Add example for deserialisation using typemap
alcaeus 3654358
Remove unnecessary batchSize option
alcaeus 7ad01fa
Use __DIR__ instead of dirname(__FILE__)
alcaeus 334bfee
Fix capitalisation for TypeMap
alcaeus 168225b
Simplify persistable example models
alcaeus 2f2d180
Add note about replica set requirements
alcaeus fb314f5
Add note about examples to the documentation
alcaeus 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 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
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,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Examples; | ||
|
||
use MongoDB\Client; | ||
|
||
use function assert; | ||
use function getenv; | ||
use function is_object; | ||
use function MongoDB\BSON\fromPHP; | ||
use function MongoDB\BSON\toRelaxedExtendedJSON; | ||
use function printf; | ||
use function rand; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
function toJSON(object $document): string | ||
{ | ||
return toRelaxedExtendedJSON(fromPHP($document)); | ||
} | ||
|
||
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); | ||
|
||
$collection = $client->test->coll; | ||
$collection->drop(); | ||
|
||
$documents = []; | ||
|
||
for ($i = 0; $i < 100; $i++) { | ||
$documents[] = ['randomValue' => rand(0, 1000)]; | ||
} | ||
|
||
$collection->insertMany($documents); | ||
|
||
$pipeline = [ | ||
[ | ||
'$group' => [ | ||
'_id' => null, | ||
'totalCount' => ['$sum' => 1], | ||
'evenCount' => [ | ||
'$sum' => ['$mod' => ['$randomValue', 2]], | ||
], | ||
'oddCount' => [ | ||
'$sum' => ['$subtract' => [1, ['$mod' => ['$randomValue', 2]]]], | ||
], | ||
'maxValue' => ['$max' => '$randomValue'], | ||
'minValue' => ['$min' => '$randomValue'], | ||
], | ||
], | ||
]; | ||
|
||
$cursor = $collection->aggregate($pipeline); | ||
|
||
foreach ($cursor as $document) { | ||
assert(is_object($document)); | ||
printf("%s\n", toJSON($document)); | ||
} |
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,78 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Examples; | ||
|
||
use MongoDB\Client; | ||
|
||
use function assert; | ||
use function getenv; | ||
use function is_object; | ||
use function MongoDB\BSON\fromPHP; | ||
use function MongoDB\BSON\toRelaxedExtendedJSON; | ||
use function printf; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
function toJSON(object $document): string | ||
{ | ||
return toRelaxedExtendedJSON(fromPHP($document)); | ||
} | ||
|
||
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); | ||
|
||
$collection = $client->test->coll; | ||
$collection->drop(); | ||
|
||
$documents = []; | ||
|
||
for ($i = 0; $i < 10; $i++) { | ||
$documents[] = ['x' => $i]; | ||
} | ||
|
||
$collection->insertMany($documents); | ||
|
||
$collection->bulkWrite( | ||
[ | ||
[ | ||
'deleteMany' => [ | ||
['x' => ['$gt' => 7]], // Filter | ||
], | ||
], | ||
[ | ||
'deleteOne' => [ | ||
['x' => 4], // Filter | ||
], | ||
], | ||
[ | ||
'replaceOne' => [ | ||
['x' => 1], // Filter | ||
['y' => 1], // Replacement | ||
], | ||
], | ||
[ | ||
'updateMany' => [ | ||
['x' => ['$gt' => 5]], // Filter | ||
['$set' => ['updateMany' => true]], // Update | ||
], | ||
], | ||
[ | ||
'updateOne' => [ | ||
['x' => 2], // Filter | ||
['$set' => ['y' => 2]], // Update | ||
], | ||
], | ||
[ | ||
'insertOne' => [ | ||
['x' => 10], // Document | ||
], | ||
], | ||
] | ||
); | ||
|
||
$cursor = $collection->find([]); | ||
|
||
foreach ($cursor as $document) { | ||
assert(is_object($document)); | ||
printf("%s\n", toJSON($document)); | ||
} |
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
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
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,109 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Examples; | ||
|
||
use MongoDB\BSON\ObjectId; | ||
use MongoDB\BSON\Persistable; | ||
use MongoDB\Client; | ||
use MongoDB\Model\BSONArray; | ||
use UnexpectedValueException; | ||
|
||
use function getenv; | ||
use function var_dump; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
class PersistableEntry implements Persistable | ||
{ | ||
/** @var ObjectId */ | ||
private $id; | ||
|
||
/** @var string */ | ||
public $name; | ||
|
||
/** @var array<PersistableEmail> */ | ||
public $emails = []; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->id = new ObjectId(); | ||
$this->name = $name; | ||
} | ||
|
||
public function getId(): ObjectId | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function bsonSerialize(): object | ||
{ | ||
return (object) [ | ||
'_id' => $this->id, | ||
'name' => $this->name, | ||
'emails' => $this->emails, | ||
]; | ||
} | ||
|
||
public function bsonUnserialize(array $data): void | ||
{ | ||
if (! $data['_id'] instanceof ObjectId) { | ||
throw new UnexpectedValueException('_id field is not of the expected type'); | ||
} | ||
|
||
if (! $data['emails'] instanceof BSONArray) { | ||
throw new UnexpectedValueException('emails field is not of the expected type'); | ||
} | ||
|
||
$this->id = $data['_id']; | ||
$this->name = (string) $data['name']; | ||
|
||
/** @psalm-suppress MixedPropertyTypeCoercion */ | ||
$this->emails = $data['emails']->getArrayCopy(); // Emails will be passed as a BSONArray instance | ||
} | ||
} | ||
|
||
class PersistableEmail implements Persistable | ||
{ | ||
/** @var string */ | ||
public $type; | ||
|
||
/** @var string */ | ||
public $address; | ||
|
||
public function __construct(string $type, string $address) | ||
{ | ||
$this->type = $type; | ||
$this->address = $address; | ||
} | ||
|
||
public function bsonSerialize(): object | ||
{ | ||
return (object) [ | ||
'type' => $this->type, | ||
'address' => $this->address, | ||
]; | ||
} | ||
|
||
public function bsonUnserialize(array $data): void | ||
{ | ||
$this->type = (string) $data['type']; | ||
$this->address = (string) $data['address']; | ||
} | ||
} | ||
|
||
$entry = new PersistableEntry('alcaeus'); | ||
$entry->emails[] = new PersistableEmail('work', 'alcaeus@example.com'); | ||
$entry->emails[] = new PersistableEmail('private', 'secret@example.com'); | ||
|
||
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); | ||
|
||
$collection = $client->test->coll; | ||
$collection->drop(); | ||
|
||
$collection->insertOne($entry); | ||
|
||
$foundEntry = $collection->findOne([]); | ||
|
||
/** @psalm-suppress ForbiddenCode */ | ||
var_dump($foundEntry); | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
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.
FWIW,
Collection::find()
can be called without arguments as$filter
defaults to an empty array. This technically violates the CRUD spec but we snuck that past the goalie long ago. Up to you if you'd like to keep this as-is or change tofind()
.Likewise in
with_transaction.php
.