A MongoDB object document mapper for PHP

Installs: 95

Dependents: 0

Stars: 4

Watchers: 1

Language: PHP

0.6.13 2013-04-14 19:38 UTC


Build Status

PHP version

A MongoDB object document mapper for PHP

Inspired by Mongoid for Ruby


  • PHP 5.4
  • MongoDB driver for PHP (min. 1.2.0)
  • Composer


Add Mango to your project
$ php composer.phar require webcodr/mango:*
Create a document class

namespace Mango\Tests\Document;

use Mango\Document;
use Mango\DocumentInterface;

class User implements DocumentInterface
    use Document;

    private function addFields()
        $this->addField('name', ['type' => 'String']);
        $this->addField('email', ['type' => 'String']);

                'type' => 'DateTime',
                'index' => true,
                'default' => 'now'

                'type' => 'DateTime',
                'index' => true,
                'default' => 'now'

You don't have to set a collection name. Mango uses the class name in lower case as collection name.

If you want to set a custom collection name, just override the method getCollectionName() in your own document classes.

There's no need to provide an id. Mango's document base class adds automatically the property '_id' with a fresh MongoId object.

Save a document

use Mango\Mango;
use Mango\DocumentManager;

use Document\User;

$mango = new Mango('mongodb://devserver:27017/galactica-actual');
$dm = new DocumentManager($mango);
$user = new User();
$user->name = 'William Adama';
$user->email 'william.adama@galactica.colonial-forces.gov';
Remove a document

The methods find and where return a \Mango\Persistence\Cursor object or an object of the class \Collection\MutableMap. It depends on which method is called.

MutableMap is part of another WebCodr project called Collection. It provides several classes to replace PHP arrays and is much more fun to use. Check it out here.

Mango uses object hydration to automatically provide a result with document objects.

Find documents by id One id
$user = User::find('abc')->first();
Multiple ids
$user = User::find('abc', 'def', 'ghi')->first();
Find all documents in collection
User::where()->each(function($user) {
    echo $user->name;
Find a document with certain field value
$user = User::where(['name' => 'William Adama']);
echo $user->count(); // result = 1
echo $user->first()->email; // result = william.adama@galactica.colonial-forces.gov