Getting Started

Marc Qualie edited this page Feb 7, 2013 · 2 revisions
Clone this wiki locally

MongoMinify is designed to be a drop in replacement for the standard MongoDB PHP Driver, which means you can use it in exactly the same way. Below is a full example of how to put data in, and read it back.


The best way to install this library is through composer

  "require": {
    "marcqualie/mongominify": "dev-master"


This file will go in ./schema/mongominify.example.php. Schema files are named as ./[db].[collection].php and are matched up when the library is first initialized.

return [
  'user_name' => [
    'short' => 'u'
  'address' => [
    'short' => 'a',
    'subset' => [
      'street' => [
        'short' => 's'
      'country' => [
        'short' => 'c'

You can also override schemas and share them using the below code. You can either override just the collection name, or specify dot syntax to override the db setting aswel.

$collection->setSchemaByName('test2'); // this will be mapped as [$collection->db].test2

PHP Usage

This code will save data to the example collection of the mongominify database and read it back

include __DIR__ . '/vendor/autoload.php';
$client = new MongoMinify\Client('mongodb://', ['connect' => true]);
$client->schema_dir = __DIR__ . '/schema';
$collection = $client->mongominify->example;
$document = [
  'user_name' => 'marcqualie',
  'address' => [
    'street' => 'Elm Street',
    'country' => 'GB'
$lookup = $collection->findOne(['_id' => $document['_id']]);
echo $lookup['address']['country']; // GB

Internal Storage

The above document will actually be stored in the database as:

{"_id":MongoId(...),"u":"marcqualie","a":{"s":"Elm Street","c":"GB"}}