Simple and swift MongoDB abstraction.
PHP
Latest commit 4df18d9 Apr 15, 2016 @bcrowe bcrowe Merge pull request #38 from phofmann-trust/patch-1
Fixed example code for nested queries
Permalink
Failed to load latest commit information.
src/League update addToSet function in order to support mixed dataset. also supp… Feb 19, 2016
tests Use local db to test hasDatabase Nov 17, 2015
.gitattributes Added a .gitattributes Oct 22, 2014
.gitignore ignore the coverage dir, for code-coverage html output Nov 8, 2012
.scrutinizer.yml
.travis.yml Add CS build to Travis Sep 17, 2015
CHANGELOG.md
CONDUCT.md Update COC Nov 13, 2015
CONTRIBUTING.md Add CONTRIBUTING.md, update LICENSE to use markdown, update LICENSE l… Nov 4, 2014
LICENSE.md Update license year Jan 5, 2016
README.md Fixed example code for nested queries Apr 15, 2016
composer.json Use space indentation in composer.json Dec 1, 2015
phpunit.xml Use spaces for indentation in phpunit.xml Nov 22, 2015

README.md

Monga

Latest Version on Packagist Software License Build Status Total Downloads

A simple and swift MongoDB abstraction layer for PHP 5.4+

What's this all about?

  • An easy API to get connections, databases and collections.
  • A filter builder that doesn't make your mind go nuts.
  • All sorts of handy update functions.
  • An abstraction for sorting single results.
  • GridFS support for a Mongo filesystem.
  • Easy aggregation and distinct values.

Vision

Monga was created with the acknowledgment of the MongoDB PHP package already being pretty awesome. That's why in a lot of cases Monga is just a simple wrapper around the MongoDB classes. It provides some helpers and helps you set up queries using a query builder. Which you can also choose not to use! Everything will still work accordingly. During the development, a lot of planning has gone into creating a nice streamlined API that closely follows the MongoDB base classes, while complementing existing query builders for SQL-like databases.

Install

Via Composer

$ composer require league/monga

Usage


use League\Monga;

// Get a connection
$connection = Monga::connection($dns, $connectionOptions);

// Get the database
$database = $connection->database('db_name');

// Drop the database
$database->drop();

// Get a collection
$collection = $database->collection('collection_name');

// Drop the collection
$collection->drop();

// Truncate the collection
$collection->truncate();

// Insert some values into the collection
$insertIds = $collection->insert([
    [
        'name' => 'John',
        'surname' => 'Doe',
        'nick' => 'The Unknown Man',
        'age' => 20,
    ],
    [
        'name' => 'Frank',
        'surname' => 'de Jonge',
        'nick' => 'Unknown',
        'nik' => 'No Man',
        'age' => 23,
    ],
]);

// Update a collection
$collection->update(function ($query) {
    $query->increment('age')
        ->remove('nik')
        ->set('nick', 'FrenkyNet');
});

// Find Frank
$frank = $collection->findOne(function ($query) {
    $query->where('name', 'Frank')
        ->whereLike('surname', '%e Jo%');
});

// Or find him using normal array syntax
$frank = $collection->find([
    'name' => 'Frank',
    'surname' => new MongoRegex('/e Jo/imxsu')
]);

$frank['age']++;

$collection->save($frank);

// Also supports nested queries
$users = $collection->find(function ($query) {
    $query->where(function ($query) {
        $query->where('name', 'Josh')
            ->orWhere('surname', 'Doe');
    })->orWhere(function ($query) {
        $query->where('name', 'Frank')
            ->where('surname', 'de Jonge');
    });
});

// get the users as an array
$arr = $users->toArray();

Aggregation

A big part of the newly released MongoDB pecl package is aggregation support. Which is super easy to do with Monga:

$collection->aggregate(function ($a) {
    $a->project([
        'name' => 1,
        'surname' => -1,
        'tags' => 1,
    ])->unwind('tags');

    // But also more advanced groups/projections
    $a->project(function ($p) {
        $p->select('field')
            ->select('scores')
            ->exclude('other_field');
    })->group(function ($g) {
        $g->by(['$name', '$surname'])
            ->sum('scores');
    });
});

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email bryan@bryan-crowe.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.