The rightweight data mapper for mongoDB and PHP 5.3+

Installs: 8 233

Dependents: 1

Stars: 4

Watchers: 42

Open Issues: 5

Language: PHP

1.0.5 2013-11-21 01:20 UTC


Moa is an Object - Document Mapper, built to persist business domain objects in mongoDB.

Build Status


  • Does the bare minimum to be useful
  • Mongo query methods are already pretty good, all that's missing is some validation and typed de/serialisation
  • Built to integrate easily into existing applications
  • Zero dependencies
  • Well tested
  • Easy to create custom types with de/serialisation behaviours and validation rules


Getting Started

In your app start up code, add something that looks like the following:


$connection = new Mongo(); // or whatever

Optionally, a callback can be provided to lazily connect on demand:


Moa::setup(function() {
    $connection = new Mongo();
    return $connection->someDb; 

Also, extra databases can be configured:


$connection = new Mongo(); // or whatever
Moa::instance()->addDatabase('anotherDb', $connection->differentDb); // also takes a callback

Defining Models

Model classes can be defined like so:


class TestDocument extends Moa\DomainObject
    public function properties()
        return array(
            'myInt' => new Moa\Types\IntegerField(array('required' => true)),            
            'myString' => new Moa\Types\StringField(),
            'myArray' => new Moa\Types\ArrayField(),
            'myOwnSelf' => new Moa\Types\EmbeddedDocumentField(array('type'=>'TestDocument')),
  • For a full list of field types and their behaviours, see the Moa\Types namespace
  • Indexes may also be defined (override DomainObject::indexes())
  • Domain objects can also specify the database they want to persist to (override DomainObject::getDatabaseName())


The query syntax is the same as the default PHP mongo driver, but accessed statically from the domain object you hope to query, eg


$docs = TestDocument::find(array('myString'=>'value'));

// it is also possible to use cursor methods
$docs = TestDocument::find(array('myString'=>'value'))->skip(20)->limit(10);

// findOne also works
$doc = TestDocument::findOne(array('myString'=>'value')); // this could except

// Documents may be saved via a call to save()
$doc->myInt = 123;

// Documents can be deleted
TestDocument::remove(array('myString' => 'value')); // Deletes all documents with a field 'myString' with value of 'value'