Giter VIP home page Giter VIP logo

monga's Introduction

Monga Build Status

A simple and swift MongoDB abstraction layer for PHP5.3+

Find Monga on Packagist/Composer

What's this all about?

  • An easy API to get connections, databases and collection.
  • 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 acknowledgement 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 cal also choose not to use! All will still works 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 builder for SQL-like database.

Examples

// Get a connection
$connection = Monga::connection($dsn, $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(array(
	array(
		'name' => 'John',
		'surname' => 'Doe',
		'nick' => 'The Unknown Man',
		'age' => 20,
	),
	array(
		'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(array('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->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(array(
		'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(array('$name', '$surname'))
			->sum('scores');
	});
});

If you need any help, come find me in the IRC channels (#fuelphp/#cabinet/#mongodb by the nick of: FrenkyNet)

Enjoy!

monga's People

Contributors

frankdejonge avatar pborreli avatar dragoonis avatar

Watchers

James Cloos avatar Rafique avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.