Commit a887e0e3 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #17 from jmikola/0.2-docs

Update docs and code examples for 0.2
parents 34bdc8a7 f13161a9
.PHONY: apigen composer test docs mkdocs .PHONY: apigen composer test docs mkdocs
MONGODB_LIB_VERSION=`php -r 'require "src/Collection.php"; echo MongoDB\Collection::VERSION, "\n";'`
COMPOSER_ARGS=update --no-interaction --prefer-source COMPOSER_ARGS=update --no-interaction --prefer-source
PHPUNIT_ARGS=--process-isolation PHPUNIT_ARGS=--process-isolation
...@@ -43,26 +42,24 @@ apigen: ...@@ -43,26 +42,24 @@ apigen:
mkdocs: mkdocs:
@command -v mkdocs >/dev/null 2>&1; \ @command -v mkdocs >/dev/null 2>&1; \
if test $$? -eq 0; then \ if test $$? -eq 0; then \
mkdocs build --clean \ mkdocs build --clean \
else \ else \
echo "Cannot find mkdocs :("; \ echo "Cannot find mkdocs :("; \
echo "Aborting."; \ echo "Aborting."; \
exit 1; \ exit 1; \
fi fi
docs-api: apigen docs-api: apigen
docs: mkdocs docs: mkdocs
release/%: release-log/%
release: test RELEASE
@echo "Please run:" @echo "Please run:"
@echo " " git add RELEASE-$(MONGODB_LIB_VERSION) @echo " " git add RELEASE-$(*)
@echo " " git commit -m \"Add $(MONGODB_LIB_VERSION) release notes\" @echo " " git commit -m \"Add $(*) release notes\"
@echo " " git tag -a -m \"Release MongoDB library $(MONGODB_LIB_VERSION)\" $(MONGODB_LIB_VERSION) @echo " " git tag -a -m \"Release MongoDB library $(*)\" $(*)
@echo " " git push --tags @echo " " git push --tags
@echo " " make release-docs @echo " " make release-docs
@echo "And don't forget to bump version in src/Collection.php"
docs: docs:
mkdocs build --clean mkdocs build --clean
...@@ -70,6 +67,5 @@ docs: ...@@ -70,6 +67,5 @@ docs:
release-docs: docs release-docs: docs
mkdocs gh-deploy --clean mkdocs gh-deploy --clean
RELEASE: release-log/%:
@git log --pretty=format:"%ad %an <%ae>%n%x09* %s%n" --date short --since="$$(git show -s --format=%ad `git rev-list --tags --max-count=1`)" > RELEASE-$(MONGODB_LIB_VERSION) @git log --pretty=format:"%ad %an <%ae>%n%x09* %s%n" --date short --no-merges --since="$$(git show -s --format=%ad `git rev-list --tags --max-count=1`)" > RELEASE-$(*)
...@@ -30,7 +30,7 @@ The preferred method of installing this library is with ...@@ -30,7 +30,7 @@ The preferred method of installing this library is with
[Composer](https://getcomposer.org/) by running the following from your project [Composer](https://getcomposer.org/) by running the following from your project
root: root:
$ composer require "mongodb/mongodb=0.2.x-dev" $ composer require "mongodb/mongodb=^0.2.0"
## Generated API Docs ## Generated API Docs
......
# Usage # Usage
## Client class
`MongoDB\Client` serves as an entry point for the library and driver. It is
constructed with the same arguments as the driver's `MongoDB\Driver\Manager`
class, which it composes. The Client class provides methods for creating a
Database or Collection class (from the internal manager instance), as well as
top-level operations, such as enumerating and dropping databases.
## Collection class ## Collection class
`MongoDB\Collection` is perhaps the most useful class in this library. It `MongoDB\Collection` is perhaps the most useful class in this library. It
provides methods for common operations on a collection, such as inserting provides methods for common operations on a collection, such as inserting
documents, querying, updating, counting, etc. documents, querying, updating, counting, etc.
Constructing a `MongoDB\Collection` requires a `MongoDB\Manager` and a namespace Constructing a `MongoDB\Collection` requires a `MongoDB\Driver\Manager` and a
for the collection. A [MongoDB namespace](http://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace) namespace for the collection. A [MongoDB namespace](http://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace)
consists of a database name and collection name joined by a dot. `examples.zips` consists of a database name and collection name joined by a dot. `examples.zips`
is on example of a namespace. A [write concern](http://docs.mongodb.org/manual/core/write-concern/) is one example of a namespace. Through normal use of the library, a Collection
will typically be created via the `selectCollection()` method on the Manager or
Database classes.
A [write concern](http://docs.mongodb.org/manual/core/write-concern/)
and [read preference](http://docs.mongodb.org/manual/core/read-preference/) may and [read preference](http://docs.mongodb.org/manual/core/read-preference/) may
also be provided when constructing a Collection (if omitted, the Collection will also be provided when constructing a Collection. If these options are omitted,
use the Manager's values as its defaults). the Collection will inherit them from the parent through which it was selected,
or the Manager.
## Finding a specific document ### Finding a specific document
``` ```
<?php <?php
...@@ -51,3 +64,14 @@ array(5) { ...@@ -51,3 +64,14 @@ array(5) {
string(2) "CA" string(2) "CA"
} }
``` ```
## Database class
`MongoDB\Database` provides methods for common operations on a database, such
as creating, enumerating, and dropping collections.
A [write concern](http://docs.mongodb.org/manual/core/write-concern/)
and [read preference](http://docs.mongodb.org/manual/core/read-preference/) may
also be provided when constructing a Database. If these options are omitted,
the Database will inherit them from the Client through which it was selected,
or the Manager.
<?php
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
// Dependencies were installed with Composer and this is the main project
$loader = require_once __DIR__ . '/../vendor/autoload.php';
} elseif (file_exists(__DIR__ . '/../../../../autoload.php')) {
// We're installed as a dependency in another project's `vendor` directory
$loader = require_once __DIR__ . '/../../../../autoload.php';
} else {
throw new Exception('Can\'t find autoload.php. Did you install dependencies with Composer?');
}
<?php <?php
require __DIR__ . "/" . "../vendor/autoload.php";
function dumpWriteResults(MongoDB\WriteResult $result) { require_once __DIR__ . "/bootstrap.php";
printf("Inserted %d documents, upserted %d, updated %d and deleted %d\n",
$result->getInsertedCount(), $result->getUpsertedCount(), $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$result->getModifiedCount(), $result->getDeletedCount() $collection = new MongoDB\Collection($manager, "phplib_demo.bulkwrite");
function dumpWriteResults(MongoDB\BulkWriteResult $result)
{
printf("Inserted %d documents, upserted %d, updated %d, and deleted %d\n",
$result->getInsertedCount(),
$result->getUpsertedCount(),
$result->getModifiedCount(),
$result->getDeletedCount()
); );
if ($result->getUpsertedCount()) { if ($result->getUpsertedCount()) {
foreach ($result->getUpsertedIds() as $index => $id) { foreach ($result->getUpsertedIds() as $index => $id) {
printf("upsertedId[%d]: %s", $index, $id); printf("upsertedId[%d]: %s\n", $index, $id);
} }
} }
} }
function dumpCollection($collection) {
printf("\n---\nDumping all documents in: %s.%s\n", function dumpCollection($collection)
{
printf("Dumping all documents in: %s.%s\n",
$collection->getDatabaseName(), $collection->getDatabaseName(),
$collection->getCollectionName() $collection->getCollectionName()
); );
...@@ -26,9 +35,6 @@ function dumpCollection($collection) { ...@@ -26,9 +35,6 @@ function dumpCollection($collection) {
printf("Found %d documents\n", $n); printf("Found %d documents\n", $n);
} }
$manager = new MongoDB\Manager("mongodb://localhost:27017");
$collection = new MongoDB\Collection($manager, "crud.bulkWrite");
$result = $collection->bulkWrite([ $result = $collection->bulkWrite([
[ [
"insertOne" => [ "insertOne" => [
...@@ -61,8 +67,9 @@ $result = $collection->bulkWrite([ ...@@ -61,8 +67,9 @@ $result = $collection->bulkWrite([
]); ]);
dumpWriteResults($result); dumpWriteResults($result);
echo "\n";
dumpCollection($collection); dumpCollection($collection);
echo "\n";
$result = $collection->bulkWrite([ $result = $collection->bulkWrite([
[ [
...@@ -84,9 +91,10 @@ $result = $collection->bulkWrite([ ...@@ -84,9 +91,10 @@ $result = $collection->bulkWrite([
], ],
]); ]);
echo "\n\n";
dumpWriteResults($result); dumpWriteResults($result);
echo "\n";
dumpCollection($collection); dumpCollection($collection);
echo "\n";
$result = $collection->bulkWrite([ $result = $collection->bulkWrite([
[ [
...@@ -96,7 +104,6 @@ $result = $collection->bulkWrite([ ...@@ -96,7 +104,6 @@ $result = $collection->bulkWrite([
], ],
]); ]);
echo "\n\n";
dumpWriteResults($result); dumpWriteResults($result);
echo "\n";
dumpCollection($collection); dumpCollection($collection);
<?php
$file = "http://media.mongodb.org/zips.json";
$zips = file($file, FILE_IGNORE_NEW_LINES);
$batch = new MongoDB\WriteBatch(true);
foreach($zips as $string) {
$document = json_decode($string);
$batch->insert($document);
}
$manager = new MongoDB\Manager("mongodb://localhost");
$result = $manager->executeWriteBatch("examples.zips", $batch);
printf("Inserted %d documents\n", $result->getInsertedCount());
?>
<?php <?php
require __DIR__ . "/../src/QueryFlags.php"; require_once __DIR__ . "/bootstrap.php";
require __DIR__ . "/../src/CursorType.php";
require __DIR__ . "/../src/InsertResult.php"; $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
require __DIR__ . "/../src/DeleteResult.php"; $collection = new MongoDB\Collection($manager, "phplib_demo.write");
require __DIR__ . "/../src/UpdateResult.php";
require __DIR__ . "/../src/Collection.php"; $hannes = [
"name" => "Hannes",
"nick" => "bjori",
$manager = new MongoDB\Manager("mongodb://localhost:27017"); "citizen" => "Iceland",
];
$hayley = [
$collection = new MongoDB\Collection($manager, "crud.examples"); "name" => "Hayley",
$hannes = array( "nick" => "Ninja",
"name" => "Hannes", "citizen" => "USA",
"nick" => "bjori", ];
"citizen" => "Iceland", $bobby = [
);
$hayley = array(
"name" => "Hayley",
"nick" => "Ninja",
"citizen" => "USA",
);
$bobby = array(
"name" => "Robert Fischer", "name" => "Robert Fischer",
"nick" => "Bobby Fischer", "nick" => "Bobby Fischer",
"citizen" => "USA", "citizen" => "USA",
); ];
$kasparov = array( $kasparov = [
"name" => "Garry Kimovich Kasparov", "name" => "Garry Kimovich Kasparov",
"nick" => "Kasparov", "nick" => "Kasparov",
"citizen" => "Russia", "citizen" => "Russia",
); ];
$spassky = array( $spassky = [
"name" => "Boris Vasilievich Spassky", "name" => "Boris Vasilievich Spassky",
"nick" => "Spassky", "nick" => "Spassky",
"citizen" => "France", "citizen" => "France",
); ];
try { try {
$result = $collection->insertOne($hannes); $result = $collection->insertOne($hannes);
printf("Inserted _id: %s\n", $result->getInsertedId()); printf("Inserted _id: %s\n\n", $result->getInsertedId());
$result = $collection->insertOne($hayley); $result = $collection->insertOne($hayley);
printf("Inserted _id: %s\n", $result->getInsertedId()); printf("Inserted _id: %s\n\n", $result->getInsertedId());
$result = $collection->insertOne($bobby); $result = $collection->insertOne($bobby);
printf("Inserted _id: %s\n", $result->getInsertedId()); printf("Inserted _id: %s\n\n", $result->getInsertedId());
$count = $collection->count(array("nick" => "bjori")); $count = $collection->count(["nick" => "bjori"]);
printf("Searching for nick => bjori, should have only one result: %d\n", $count); printf("Searching for nick => bjori, should have only one result: %d\n\n", $count);
$result = $collection->updateOne( $result = $collection->updateOne(
array("citizen" => "USA"), ["citizen" => "USA"],
array('$set' => array("citizen" => "Iceland")) ['$set' => ["citizen" => "Iceland"]]
); );
printf("Updated: %s (out of expected 1)\n", $result->getModifiedCount()); printf("Updated: %s (out of expected 1)\n\n", $result->getModifiedCount());
$result = $collection->find(array("citizen" => "Iceland"), array("comment" => "Excellent query")); $cursor = $collection->find(
["citizen" => "Iceland"],
["comment" => "Excellent query"]
);
echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n"; echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n";
foreach($result as $document) { foreach($cursor as $document) {
var_dump($document); var_dump($document);
} }
echo "\n";
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit; exit;
} }
try { try {
$result = $collection->find(); $cursor = $collection->find();
echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n"; echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n";
foreach($result as $document) { foreach($cursor as $document) {
var_dump($document); var_dump($document);
} }
echo "\n";
$result = $collection->distinct("citizen"); $result = $collection->distinct("citizen");
echo "Distinct countries:\n"; echo "Distinct countries:\n";
var_dump($result); var_dump($result);
echo "\n";
echo "aggregate\n"; echo "aggregate\n";
$aggregate = $collection->aggregate(array(array('$project' => array("name" => 1, "_id" => 0))), array("useCursor" => true, "batchSize" => 2)); $result = $collection->aggregate(
[
['$project' => ["name" => 1, "_id" => 0]],
],
[ "useCursor" => true, "batchSize" => 2 ]
);
printf("Should be 3 different people\n"); printf("Should be 3 different people\n");
foreach($aggregate as $person) { foreach($result as $person) {
var_dump($person); var_dump($person);
} }
echo "\n";
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit; exit;
...@@ -90,15 +99,15 @@ try { ...@@ -90,15 +99,15 @@ try {
try { try {
$result = $collection->updateMany( $result = $collection->updateMany(
array("citizen" => "Iceland"), ["citizen" => "Iceland"],
array('$set' => array("viking" => true)) ['$set' => ["viking" => true]]
); );
printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount()); printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount());
$result = $collection->find(); $result = $collection->find();
foreach($result as $document) { foreach($result as $document) {
var_dump($document); var_dump($document);
} }
echo "\n";
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit; exit;
...@@ -106,16 +115,16 @@ try { ...@@ -106,16 +115,16 @@ try {
try { try {
echo "This is the trouble maker\n";
$result = $collection->replaceOne( $result = $collection->replaceOne(
array("nick" => "Bobby Fischer"), ["nick" => "Bobby Fischer"],
array("name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway") ["name" => "Magnus Carlsen", "nick" => "unknown", "citizen" => "Norway"]
); );
printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getModifiedCount()); printf("Replaced: %d (out of expected 1), verify Bobby has been replaced with Magnus\n", $result->getModifiedCount());
$result = $collection->find(); $result = $collection->find();
foreach($result as $document) { foreach($result as $document) {
var_dump($document); var_dump($document);
} }
echo "\n";
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit; exit;
...@@ -124,34 +133,46 @@ try { ...@@ -124,34 +133,46 @@ try {
try { try {
$result = $collection->deleteOne($document); $result = $collection->deleteOne($document);
printf("Deleted: %d (out of expected 1)\n", $result->getDeletedCount()); printf("Deleted: %d (out of expected 1)\n\n", $result->getDeletedCount());
$result = $collection->deleteMany(array("citizen" => "Iceland")); $result = $collection->deleteMany(["citizen" => "Iceland"]);
printf("Deleted: %d (out of expected 2)\n", $result->getDeletedCount()); printf("Deleted: %d (out of expected 2)\n\n", $result->getDeletedCount());
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit; exit;
} }
try { try {
echo "FindOneAndReplace\n"; echo "FindOneAndReplace\n";
$result = $collection->findOneAndReplace($spassky, $kasparov, array("upsert" => true)); $result = $collection->findOneAndReplace(
$spassky,
$kasparov,
["upsert" => true]
);
echo "Kasparov\n"; echo "Kasparov\n";
var_dump($result); var_dump($result);
echo "\n";
echo "Returning the old document where he was Russian\n"; echo "Returning the old document where he was Russian\n";
$result = $collection->findOneAndUpdate($kasparov, array('$set' => array("citizen" => "Croatia"))); $result = $collection->findOneAndUpdate(
$kasparov,
['$set' => ["citizen" => "Croatia"]]
);
var_dump($result); var_dump($result);
echo "\n";
echo "Deleting him, he isn't Croatian just yet\n"; echo "Deleting him, he isn't Croatian just yet\n";
$result = $collection->findOneAndDelete(array("citizen" => "Croatia")); $result = $collection->findOneAndDelete(["citizen" => "Croatia"]);
var_dump($result); var_dump($result);
echo "\n";
echo "This should be empty\n"; echo "This should be empty\n";
$result = $collection->find(array()); $result = $collection->find();
foreach($result as $document) { foreach($result as $document) {
var_dump($document); var_dump($document);
} }
echo "\n";
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
exit; exit;
...@@ -162,7 +183,7 @@ try { ...@@ -162,7 +183,7 @@ try {
$result = $collection->bulkWrite( $result = $collection->bulkWrite(
// Required writes param (an array of operations) // Required writes param (an array of operations)
[ [
// Like explain(), operations identified by single key // Operations identified by single key
[ [
'insertOne' => [ 'insertOne' => [
['x' => 1] ['x' => 1]
...@@ -204,11 +225,10 @@ try { ...@@ -204,11 +225,10 @@ try {
printf("deletedCount: %d\n", $result->getDeletedCount()); printf("deletedCount: %d\n", $result->getDeletedCount());
foreach ($result->getUpsertedIds() as $index => $id) { foreach ($result->getUpsertedIds() as $index => $id) {
printf("upsertedId[%d]: %s", $index, $id); printf("upsertedId[%d]: %s\n", $index, $id);
} }
} catch(Exception $e) { } catch(Exception $e) {
printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
echo $e->getTraceAsString(), "\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