CakePHP, MongoDB primer

For your reference, and mine as well.

FIrst you need MongoDB to run on your system. It’s always easy on Debian/Ubuntu. Simply add:

deb 9.4 10gen

to your /etc/apt/sources.lst for Ubuntu Jaunty (9.4).

$ sudo apt-get update; sudo apt-get install mongodb-stable

Once everything is downloaded — it takes awhile! — ps should show you something like:

$ ps -aef | grep mongodb
mongodb 8322 1 2 17:49 ? 00:02:59 /usr/bin/mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongodb.log run --config /etc/mongodb.conf

Next you need PECL mongo, which allows PHP to communicate with MongoDB:

$ pecl install mongo

Remember to add ‘’ to your php.ini file! For my implementation, I created a separate mongo.ini in /etc/php5/conf.d/.

And then you’ll need the MongoDB datasource:

cd path/to/cakephp/app/plugins/; git clone git:// mongodb

Alternatively if you don’t have git, you can always download the source from Once the clone (or download) is complete, you should have mongodb/ in app/plugins.

app/config/database.php should have a config like:

var $mongo = array(
'database' => 'someApp',
'driver' => 'mongodb.mongodbSource',
'host' => 'localhost',
'port' => 27017

Your model should have, at the very least:

var $mongoSchema = array('type' => 'string'),
'created' => array('type' => 'date'),
'modified' => array('type' => 'date')
var $useDbConfig = 'mongo';

For such testing, CakePHP scaffolding will come in handy. Once some data is inserted, connect to the console:

$ mongo localhost:27017/someApp
MongoDB shell version: 1.6.4
connecting to: localhost:27017/someApp
> db.somes.count();