paskvilwiki

up: databases & search

MongoDB

last updated 2014-03

installation - Ubuntu

MongoDB Inc. provides a PPA source for Ubuntu. PECL provides the native PHP driver

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
$ sudo apt-get update
$ sudo apt-get install mongodb-10gen
$ sudo apt-get install php-pear php5-dev   # if you don't have PECL yet; phpize is also needed
$ sudo pecl install mongo                  # PECL will echo what to add to php.ini;
                                           # you can add this to both CLI and in-Apache PHP using
$ echo "extension=mongo.so" | sudo tee /etc/php5/conf.d/mongo.ini
$ sudo service apache2 restart             # restart apache to reload php settings

update 2026 - all of this is long gone - the 10gen repo, apt-key, and the PHP 5 mongo extension. the server is installed from MongoDB's own repo, see the install guide, and PHP talks to it through the mongodb extension (pecl install mongodb) plus the mongodb/mongodb Composer library

troubles on desktop

while it behaves well on servers, MongoDB has some peculiarities on desktop machines

here's a typical day in the desktop MongoDB user's life:

# check it's running
$ ps aux | grep mongod

0 S user    22154 28201  0  80   0 -  1097 pipe_w 16:31 pts/9    00:00:00 grep mongod

# not running, eh?... lets try manually... beware that mongo
# can't find its own config files on desktop machines...
$ sudo mongod --config /etc/mongodb.conf

all output going to: /var/log/mongodb/mongodb.log

# did it die? if not, you're lucky and you're ready to roll!...
# otherwise, lets rock... log will most probably tell you that you
# need to repair db because of unclean shutdown
$ sudo mongod --config /etc/mongodb.conf --repair

...

# now we should be able to start mongo
$ sudo mongod --config /etc/mongodb.conf

and now you should have mongo running (and one terminal stuck with mongo… but who cares, it's not the olden days, where you had just one)

using in PHP

update 2026 - this is the old mongo extension's API. with the current mongodb/mongodb library it's new MongoDB\Client("mongodb://host:port"), ->database->collection works the same, ensureIndex() is now createIndex(), and explain() is a database command

PHP's PECL driver provides a very neat and clean integration. databases and the collections within are accessed directly as if members of the (client) connection:

// host defaults to 'localhost', port defaults to 27017:
$client = new MongoClient(["host[:port]"]);
// access database directly - replace 'database' with DB's name:
$client->database
// as well as collection within the database:
$client->database->collection
// now lets find some documents in the collection:
$client->database->collection->find([query]);
// find() returns iterator, so just foreach through it:
foreach ($client->database->collection->find([query]) as $id => $doc)
    ; // do your best here...

if you need only one document (or know that there is - at most - one), you can use the findOne() function that returns the document directly

some more useful functions:

// make sure 'field' is indexed:
$client->database->collection->ensureIndex('field');
// get some information about the query:
$client->database->collection->find([query])->explain();