simplemongophp -- a simple library for Mongo PHP

Showing 1-2 of 2 messages
simplemongophp -- a simple library for Mongo PHP Ian White 7/27/09 10:52 PM
Hello Mongolians,

For the past few months in the development of The Business Insider (, we've been using this library. We recently switched to using PHP (we've been using Mongo itself for some time now), so this code is currently in use in production. Today I finally got around to PHPDocumenting it and I figured I'd make it available in case anyone else finds it useful.

It's really nothing fancy -- just an easy way of using data objects without a lot of overhead or abstraction. If you have no interest in data objects, you still might find the Db class useful to smooth out some of the rough edges of the PHP API (at a minimum, it'll cut down on your selectCollection() calls!)

Github URL:

Sample usage:


   $mongo = new Mongo();
   define('MONGODB_NAME', 'lost');
   class LostPerson extends Dbo {
     function rollCall() {
       echo "$this->name is a " . ($this->goodguy ? 'good' : 'bad') . " guy!\n";
   Dbo::addClass('LostPerson', 'people');

   Db::batchInsert('people', array(
     array('name' => 'Jack', 'sex' => 'M', 'goodguy' => true),
     array('name' => 'Kate', 'sex' => 'F', 'goodguy' => true),
     array('name' => 'Locke', 'sex' => 'M', 'goodguy' => true),
     array('name' => 'Hurley', 'sex' => 'M', 'goodguy' => true),
     array('name' => 'Ben', 'sex' => 'M', 'goodguy' => false),
   foreach (Dbo::find('LostPerson', array('goodguy' => true), array('sort' => array('name' => 1))) as $p) {
   $ben = Dbo::findOne('LostPerson', array('name' => 'Ben'));
   $locke = Dbo::findOne('LostPerson', array('name' => 'Locke'));
   $ben->enemy = Dbo::toRef($locke);
   $ben->goodguy = null;

If the 10gen folks can add a link, I'd be much obliged.


Ian White
Eliot Horowitz 7/28/09 11:31 AM <This message has been deleted.>