Mongo Mapper

The Mongo Object-Document-Mapper (ODM) is an implementation of the abstract Active Record Cursor class. Have a look into it for inherited method descriptions.

Namespace: \DB\Mongo
File location: lib/db/mongo/mapper.php


To use the Mongo ODM, create a valid MongoDB Connection and follow this example:

$mapper = new \DB\Mongo\Mapper(\DB\Mongo $db, string $collection);

If you like to create a model class, you might like to wrap it up:

$f3->set('DB', new \DB\Mongo('mongodb://localhost:27017','fatfree'));

class User extends \DB\Mongo\Mapper {
    public function __construct() {
        parent::__construct( \Base::instance()->get('DB'), 'users' );

$user = new User();
$user->load(array('_id' => new \MongoId('50ecaa466afa2f8c1c000004')));
// etc.



The $filter argument for Mongo accepts the common mongo find array, see db.collection.find reference and query-documents tutorial.

array([ array $find ]);

Here is a simple example how to search in mongo collections:

$userList = $user->find(array('email'=> new \MongoRegex('/gmail/')));
// returns all users with an email address that contains GMAIL

// ends with gmail.com => /gmail\.com$/
// starts with john  => /^john/

Or just load a single user by its ID:

$user->load(array('_id'=> new \MongoDB\BSON\ObjectID('507c35dd8fada716c89d0013')));


The $option argument for Mongo accepts the following structure:

    'group' => array (
    'order' => string $order,
    'limit' => integer $limit,
    'offset' => integer $offset

For more details about group, go to the db.collection.group reference. And for more about order, and a look at the cursor.sort reference.



Return TRUE if the given field is defined

bool exists( string $key )


Assign a value to a field

scalar|FALSE set( string $key, scalar $val )

This class takes advantage of the Magic class and ArrayAccess interface. It means you can set and get variables with direct access like this:

$mapper->foo = 'bar';
$mapper['foo'] = 'bar';


Retrieve the value of a given field

scalar|FALSE get( string $key )


Delete a field

NULL clear( string $key )


Return the fields of a mapper object as an associative array

array cast( [ object $obj = NULL ] )


Build query and execute

array select( [ string $fields = NULL [, array $filter = NULL [, array $options = NULL [, int $ttl = 0 ]]]] )


Return records that match a given criteria

array find( [ array $filter = NULL [, array $options = NULL [, int $ttl = 0 ]]] )


Count records that match a given criteria

int count( [ array $filter = NULL [, int $ttl = 0 ]] )


Insert a new record

array insert()


Update the current record

array update()


Delete the current record

bool erase( [ array $filter = NULL ] )


Hydrate the mapper object using an array

NULL copyfrom( array | string $var [, callback $func = NULL ] )

This function allows you to hydrate the mapper using an array (or the name of a hive variable containing an array).

$func is the callback function to apply to the hive array variable:

if ($func)  $var = $func($var);


Populate hive array variable with mapper fields

NULL copyto( string $key )


Convert an array to a mapper object

protected object factory( array $row )

This protected method is used internally by the select method.