Monday, March 7, 2011

Understanding PHP Mongo

MongoDb, a NoSQL database management system, uses a PECL(PEAR Extension Class Library)  called PHP Mongo as a driver to interact with the MongoDb server. If you have pear installed, it so easy to add it to your PHP (i.e pecl install mongo) and you only have to activate it as part of your PHP modules. Setting aside the "download and installation" of PHP Mongo, let us see the what it can do for you.

Mongo - Connect to the MongoDb server


PHP Mongo uses the Mongo object to make a connection to the MongoDb server. It allows you to specify a hostname, port, user, password and other options. It has the method to directly instantiate a MongoDb object and a MongoCollection object.
$m = new Mongo("mongodb://{$username}:{$password}@{$host}");

MongoDb - Select your Working MongoDb Database

After making the connection, you can now choose your working database.
$m = new Mongo("mongodb://{$username}:{$password}@{$host}");
$db = $m->selectDb('yourdb');
From here on, many database-centric methods will become available to $db because it is now an instance of MongoDb object. Please also note that instantiating MongoDb object on its own becomes unnecessary.

MongoCollection - Select your Working MongoDb Collection

So you've selected your database. The next thing you need to do is select your working collection. Similar to how you selected the database, this will instantiate the MongoCollection object automatically.
$m = new Mongo("mongodb://{$username}:{$password}@{$host}");
$db = $m->selectDb('yourdb');
$co = $db->selectCollection('yourcollection');
This will in the same manner as the selecting the database open interfaces for interacting with your collection.

MongoCursor - Get and Manipulate Data from your Collection

Unlike MySQL which spews large packets in one go, MongoDb uses a cursor on an open stream from the connection to transfer and handle data in "smaller sets" incrementally. This built-in flexibility prevents choking of the connection but would also give the MongoDb newbie a surprise if unwary (Where's the rest of my data ???? :P). So how do you instantiate a MongoCursor? You don't have to. Just call the method "find" of your collection and it will do it for you automatically.
$m = new Mongo("mongodb://{$username}:{$password}@{$host}");
$db = $m->selectDb('yourdb');
$co = $db->selectCollection('yourcollection');
$cursor = $co->find() //has become a MongoCursor instance
If you want to get all the resulting documents from your cursor you can do this,
$cursor = $co->find() //has become a MongoCursor instance
while(FALSE !== $cursor->hasNext()) {
   print_r($cursor->getNext());
}
Or if you want to transform it entirely to an array similar to mysql_fetch_assoc, you can do the following,
$cursor = $co->find() //has become a MongoCursor instance
$results = iterator_to_array($cursor); //$results is now a multi-dimensional associative array
That's it. You can now interact and manipulate your data the way you want it.

For an implementation of Table/Collection Data Gateway, Row Data Gateway and Adapter Design Patterns wrapping the above objects in easy-to-use way much like an ORM, you can check out my project - ZFDbMongoDb. For more info, you can visit mongodb site and PHP Mongo documentation.
  • Related Links Widget for Blogspot

No comments: