Introducing MongoQB

A few years ago I released a MongoDB library for the CodeIgniter PHP framework which has become quite popular thanks to it's likeness to CodeIgniter's query builder library.

I've just spent the last week travelling and to keep myself occupied I spent some time re-architecting the library's code; removing the framework dependancy, making it Composer friendly and adding a full suite of unit tests. Check out the code on Github - https://github.com/alexbilbie/MongoQB.

Install via Packagist and Composer

Add the following into your composer.json file:

{
    "require": {
        "alexbilbie/mongoqb": "*"
    }
}

Then run

composer install

Install via Git

git clone git://git@github.com:alexbilbie/MongoQB

Download a zip/tarball

Download the latest version:

(Note the zip/tarball won't include any unit tests or composer files)

Unit tests

To run the unit test suite make sure you have MongoDB installed locally and running with no authentication and on the default port - 27017. The library currently has 100% unit test coverage.

Then run:

composer update --dev
cd vendor/alexbilbie/mongoqb
phpunit -c tests/phpunit.xml

Example usage

Connect to the database

$qb = \MongoQB\Builder(array(
    'dsn'   =>  'mongodb://user:pass@localhost:27017/databaseName'
);

Insert a document

$qb->insert('collectionName', [
    'name'  =>  'Alex',
    'age'   =>  22,
    'likes' =>  ['whisky', 'gin']
]);

Update a single document

$qb
    ->where(['name' => 'Alex'])
    ->set([
        'country' => 'UK',
        'job' => 'Developer'
    ])
    ->push('likes', ['PHP', 'coffee'])
    ->update('collectionName');

Delete a single document

$qb
    ->where(['name' => 'Alex'])
    ->delete('collectionName');

Search for matching documents

$results = $qb
    ->whereGt('age', 21)
    ->whereIn('likes', ['whisky'])
    ->where('country', 'UK')
    ->get('collectionName');

If you find any bugs please file a report in the Issue tracker