Commit 6f9139e9 authored by Jeremy Mikola's avatar Jeremy Mikola

Update documentation for PECL mongodb-0.2.0

parent 1285815a
......@@ -18,8 +18,8 @@ skipDocPrefix:
charset:
- UTF-8
main: phongo libraries
title: phongo libraries
main: MongoDB PHP library
title: MongoDB PHP library
baseUrl: http://10gen-labs.github.io/mongo-php-libraries-prototype
googleCseId: null
googleAnalytics: null
......
# Example data
# Example Data
For the purpose of this documentation we will be using the
[zips.json](http://media.mongodb.org/zips.json) in our examples.
Usage examples in this documentation will use
[zips.json](http://media.mongodb.org/zips.json). This is a dataset comprised of
United States postal codes, populations, and geographic locations.
Importing the dataset into the database can be done in several ways,
for example using the following script:
Importing the dataset into MongoDB can be done in several ways. The following
examples uses the low-level `mongodb` PHP driver:
```php
<?php
$file = "http://media.mongodb.org/zips.json";
$zips = file($file, FILE_IGNORE_NEW_LINES);
$bulk = new MongoDB\Driver\BulkWrite());
$batch = new MongoDB\WriteBatch(true);
foreach($zips as $string) {
foreach ($zips as $string) {
$document = json_decode($string);
$batch->insert($document);
$bulk->insert($document);
}
$manager = new MongoDB\Manager("mongodb://localhost");
$manager = new MongoDB\Driver\Manager("mongodb://localhost");
$result = $manager->executeWriteBatch("examples.zips", $batch);
$result = $manager->executeBulkWrite("test.zips", $bulk);
printf("Inserted %d documents\n", $result->getInsertedCount());
?>
```
Outputs
Executing this script should yield the following output:
```
Inserted 29353 documents
```
You may also import the dataset using the
[`mongoimport`](http://docs.mongodb.org/manual/reference/program/mongoimport/)
command, which is included with MongoDB:
```
$ mongoimport --db examples --collection zips --file zips.json
```
# Welcome to phongo libraries!
# MongoDB PHP Library
phongo libraries is a CRUD API ontop of [Phongo](https://github.com/10gen-labs/mongo-php-driver-prototype).
Its purpose is to provide standard MongoDB API and follows the MongoDB CRUD API Specification[1]
that all [MongoDB](http://mongodb.com) supported drivers follow.
This library provides a high-level abstraction around the lower-level
[PHP driver](https://github.com/10gen-labs/mongo-php-driver-prototype) (i.e. the
`mongodb` extension).
PHongo CRUD provides several convenience methods that abstract the core PHongo extension.
The methods include functionality to insert a single document, counting all documents in
an collection, and delete documents from a collection.
While the extension provides a limited API for executing commands, queries, and
write operations, this library implements an API similar to that of the
[legacy PHP driver](http://php.net/manual/en/book.mongo.php). It contains
abstractions for client, database, and collection objects, and provides methods
for CRUD operations and common commands (e.g. index and collection management).
If you are developing an application with MongoDB, you should consider using
this library, or another high-level abstraction, instead of the extension alone.
# Installation
As PHongo CRUD is an abstraction layer for PHongo, it naturally requires [PHongo to be
installed](http://10gen-labs.github.io/mongo-php-driver-prototype/#installation):
$ wget https://github.com/10gen-labs/mongo-php-driver-prototype/releases/download/0.1.2/phongo-0.1.2.tgz
$ pecl install phongo-0.1.2.tgz
$ echo "extension=phongo.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
For further information about the architecture of this library and the `mongodb`
extension, see:
The best way to then install PHongo CRUD is via [composer](https://getcomposer.org/)
by adding the following to
[composer.json](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup):
- http://www.mongodb.com/blog/post/call-feedback-new-php-and-hhvm-drivers
```json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/10gen-labs/mongo-php-library-prototype"
}
],
"require": {
"ext-phongo": ">=0.1.2",
"10gen-labs/mongo-php-library-prototype": "dev-master"
}
```
# Installation
and then running
As a high-level abstraction for the driver, this library naturally requires that
the [`mongodb` extension be installed](http://10gen-labs.github.io/mongo-php-driver-prototype/#installation):
```shell
$ composer install
```
$ pecl install mongodb-alpha
$ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
The preferred method of installing this library is with
[Composer](https://getcomposer.org/) by running the following from your project
root:
$ composer require "mongodb/mongodb=0.2.x-dev"
## Generated API Docs
If you are just interested in looking at the API provided, checkout the apidoc generated
documentation on: [http://10gen-labs.github.io/mongo-php-library-prototype/api/class-MongoDB.Collection.html](http://10gen-labs.github.io/mongo-php-library-prototype/api/class-MongoDB.Collection.html)
If you are just interested in referencing the API provided by this library, you
can view generated API documentation [here](./api).
## MongoDB Tutorial
MongoDB first-timer?
Checkout these links to get a quick understanding what MongoDB is, how it works, and
what the most common terms used with MongoDB mean.
- [MongoDB CRUD Introduction](http://docs.mongodb.org/manual/core/crud-introduction/)
- [What is a MongoDB Document](http://docs.mongodb.org/manual/core/document/)
- [MongoDB `dot notation`](http://docs.mongodb.org/manual/core/document/#dot-notation)
- [MongoDB ObjectId](http://docs.mongodb.org/manual/reference/object-id/)
[1] The specification has not been published yet - it is still a Work In Progress
If you are a new MongoDB user, these links should help you become more familiar
with MongoDB and introduce some of the concepts and terms you will encounter in
this documentation:
- [Introduction to CRUD operations in MongoDB](http://docs.mongodb.org/manual/core/crud-introduction/)
- [What is a MongoDB document?](http://docs.mongodb.org/manual/core/document/)
- [MongoDB's *dot notation* for accessing document properties](http://docs.mongodb.org/manual/core/document/#dot-notation)
- [ObjectId: MongoDB's document identifier](http://docs.mongodb.org/manual/reference/object-id/)
$( document ).ready(function() {
$(document).ready(function() {
$('pre code').parent().addClass('prettyprint well');
prettyPrint();
});
# Usage
## Collection class
# MongoDB\Collection
`MongoDB\Collection` is perhaps the most useful class in this library. It
provides methods for common operations on a collection, such as inserting
documents, querying, updating, counting, etc.
MongoDB\Collection is the main object of this component.
It has convenience methods for most of the _usual suspects_ tasks, such as inserting
documents, querying, updating, counting, and so on.
Constructing a `MongoDB\Collection` requires a `MongoDB\Manager` and a 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`
is on example of a namespace. 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 Collection (if omitted, the Collection will
use the Manager's values as its defaults).
Constructing a MongoDB\Collection requires a MongoDB\Manager and then namespace to operate
on. A [MongoDB namespace](http://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace)
is in the form of "databaseName.collectionName", for example: `examples.zips`.
A [WriteConcern](http://docs.mongodb.org/manual/core/write-concern/) and
[ReadPreference](http://docs.mongodb.org/manual/core/read-preference/) can also optionally
be provided, if omitted the default values from the MongoDB\Manager will be used.
## Finding a specific document
## finding a specific document
```
<?php
require __DIR__ . "/../". "vendor/autoload.php";
$manager = new MongoDB\Manager("mongodb://localhost:27017");
// This path should point to Composer's autoloader
require_once __DIR__ . "/vendor/autoload.php";
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$collection = new MongoDB\Collection($manager, "examples.zips");
$sunnyvale = $collection->findOne(array("_id" => "94086"));
var_dump($sunnyvale);
?>
```
Outputs
Executing this script should yield the following output:
```
array(5) {
["_id"]=>
......
site_name: "PHongo libraries"
site_name: "MongoDB PHP library"
repo_url: https://github.com/10gen-labs/mongo-php-library-prototype
theme: spacelab
pages:
......
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