Databases Reference
In-Depth Information
Restart any application server you might have running, and the Mongo extension will
be loaded the next time PHP starts.
Linux and Unix install
Run the following:
$ pecl install mongo
Then add the following line to your
php.ini
file:
extension=mongo.so
Restart any application server you might have running, and the Mongo extension will
be loaded the next time PHP is started.
Using the PHP Driver
The
Mongo
class is a connection to the database. By default, the constructor attempts to
connect to a database server running locally on the default port.
You can use the
__get
function to get a database from the connection and a collection
from the database (even a subcollection from a collection). For example, this connects
to MongoDB and gets the
bar
collection in the
foo
database:
<?php
$connection = new Mongo();
$collection = $connection->foo->bar;
?>
You can continue chaining getters to access subcollections. For example, to get the
bar.baz
collection, you can say the following:
$collection = $connection->foo->bar->baz;
Documents are represented by associative arrays in PHP. Thus, something like
{"foo" :
"bar"}
in JavaScript could be represented as
array("foo" => "bar")
in PHP. Arrays are
also represented as arrays in PHP, which sometimes leads to confusion:
["foo", "bar",
"baz"]
in JavaScript is equivalent to
array("foo", "bar", "baz")
.
The PHP driver uses PHP's native types for null, booleans, numbers, strings, and arrays.
For all other types, there is a Mongo-prefixed type:
MongoCollection
is a collection,
MongoDB
is a database, and
MongoRegex
is a regular expression. There is extensive
documentation in the
PHP manual
for all of these classes.