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: ...@@ -18,8 +18,8 @@ skipDocPrefix:
charset: charset:
- UTF-8 - UTF-8
main: phongo libraries main: MongoDB PHP library
title: phongo libraries title: MongoDB PHP library
baseUrl: http://10gen-labs.github.io/mongo-php-libraries-prototype baseUrl: http://10gen-labs.github.io/mongo-php-libraries-prototype
googleCseId: null googleCseId: null
googleAnalytics: null googleAnalytics: null
......
# Example data # Example Data
For the purpose of this documentation we will be using the Usage examples in this documentation will use
[zips.json](http://media.mongodb.org/zips.json) in our examples. [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, Importing the dataset into MongoDB can be done in several ways. The following
for example using the following script: examples uses the low-level `mongodb` PHP driver:
```php ```php
<?php <?php
$file = "http://media.mongodb.org/zips.json"; $file = "http://media.mongodb.org/zips.json";
$zips = file($file, FILE_IGNORE_NEW_LINES); $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); $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()); printf("Inserted %d documents\n", $result->getInsertedCount());
?> ?>
``` ```
Outputs Executing this script should yield the following output:
``` ```
Inserted 29353 documents 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). This library provides a high-level abstraction around the lower-level
Its purpose is to provide standard MongoDB API and follows the MongoDB CRUD API Specification[1] [PHP driver](https://github.com/10gen-labs/mongo-php-driver-prototype) (i.e. the
that all [MongoDB](http://mongodb.com) supported drivers follow. `mongodb` extension).
PHongo CRUD provides several convenience methods that abstract the core PHongo extension. While the extension provides a limited API for executing commands, queries, and
The methods include functionality to insert a single document, counting all documents in write operations, this library implements an API similar to that of the
an collection, and delete documents from a collection. [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 For further information about the architecture of this library and the `mongodb`
extension, see:
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*||"`
The best way to then install PHongo CRUD is via [composer](https://getcomposer.org/) - http://www.mongodb.com/blog/post/call-feedback-new-php-and-hhvm-drivers
by adding the following to
[composer.json](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup):
```json # Installation
"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"
}
```
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 $ pecl install mongodb-alpha
$ composer install $ 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 ## Generated API Docs
If you are just interested in looking at the API provided, checkout the apidoc generated If you are just interested in referencing the API provided by this library, you
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) can view generated API documentation [here](./api).
## MongoDB Tutorial ## MongoDB Tutorial
MongoDB first-timer? If you are a new MongoDB user, these links should help you become more familiar
Checkout these links to get a quick understanding what MongoDB is, how it works, and with MongoDB and introduce some of the concepts and terms you will encounter in
what the most common terms used with MongoDB mean. this documentation:
- [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
- [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');
$('pre code').parent().addClass('prettyprint well');
prettyPrint(); prettyPrint();
}); });
# Usage # 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. Constructing a `MongoDB\Collection` requires a `MongoDB\Manager` and a namespace
It has convenience methods for most of the _usual suspects_ tasks, such as inserting for the collection. A [MongoDB namespace](http://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace)
documents, querying, updating, counting, and so on. 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 ## Finding a specific document
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
``` ```
<?php <?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"); $collection = new MongoDB\Collection($manager, "examples.zips");
$sunnyvale = $collection->findOne(array("_id" => "94086")); $sunnyvale = $collection->findOne(array("_id" => "94086"));
var_dump($sunnyvale); var_dump($sunnyvale);
?> ?>
``` ```
Outputs
Executing this script should yield the following output:
``` ```
array(5) { array(5) {
["_id"]=> ["_id"]=>
......
site_name: "PHongo libraries" site_name: "MongoDB PHP library"
repo_url: https://github.com/10gen-labs/mongo-php-library-prototype repo_url: https://github.com/10gen-labs/mongo-php-library-prototype
theme: spacelab theme: spacelab
pages: 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