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.

Install

The best way to install this library is through composer

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

Schema

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
$collection->setSchemaByName('global.test');

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://127.0.0.1:27017', ['connect' => true]);
$client->schema_dir = __DIR__ . '/schema';
$collection = $client->mongominify->example;
$document = [
  'user_name' => 'marcqualie',
  'address' => [
    'street' => 'Elm Street',
    'country' => 'GB'
  ]
];
$collection->save($document);
$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"}}