shershams / lmongo
LMongo is a MongoDB package for Laravel 4.
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-11-19 04:53:03 UTC
README
LMongo is MongoDB package for Laravel 4. Most part of LMongo is based on Illuminate/Database (Thanks to @taylorotwell)
Please note that LMongo project is under heavy development, some functionalities are not yet tested. Please report if you find any bug.
Installation
Add shershams/lmongo
as a requirement to composer.json:
{ "require": { "shershams/lmongo": "*" } }
And then run composer update
Once Composer has installed or updated your packages you need to register LMongo. Open up app/config/app.php
and find the providers
key and add:
'LMongo\LMongoServiceProvider'
Then find the aliases
key and add following line to the array:
'LMongo' => 'LMongo\Facades\LMongo', 'EloquentMongo' => 'LMongo\Eloquent\Model',
Finally you need to publish a configuration file by running the following Artisan command.
$ php artisan config:publish shershams/lmongo
This will copy the default configuration file to app/config/packages/shershams/lmongo/config.php
Basic Usage
You may get a MongoDB instance by calling the LMongo::connection
method:
$LMongo = LMongo::connection();
This will give you an instance of the default MongoDB server. You may pass the server name to the connection
method to get a specific server as defined in your mongodb configuration:
$LMongo = LMongo::connection('othermongodbserver');
LMongo uses magic method to pass the collection name to the Database class and return MongoCollection instance. Then you can use any of MongoCollection methods:
$item = $LMongo->collection_name->findOne(array('key' => 'value')); $items = $LMongo->collection_name->find(array('key' => 'value'))->limit(5); $LMongo->collection_name->remove(array('key' => 'value'));
Get the MongoDB object:
$mongodb = $LMongo->getMongoDB(); $collection_names = $mongodb->getCollectionNames();
Get the MongoClient object:
$mongo = $LMongo->getMongoClient(); $databases = $mongo->listDBs();
Select/switch the database:
$LMongo->selectDB($dbName);
Create the database:
$LMongo->createDB($dbName);
Query Builder
Wheres
Retrieving All Rows From A Collection
$users = LMongo::collection('users')->get(); foreach ($users as $user) { var_dump($user['name']); }
Retrieving A Single Document From A Collection
$user = LMongo::collection('users')->where('name', 'John')->first(); var_dump($user['name']);
Retrieving A Single Column From A Document
$name = LMongo::collection('users')->where('name', 'John')->pluck('name');
Specifying A Fields
$users = LMongo::collection('users')->get(array('name', 'email'));
Using Where Operators
$users = LMongo::collection('users')->where('votes', 100)->get();
Or Statements
$users = LMongo::collection('users') ->where('votes', 100) ->orWhere('name', 'John') ->get();
Nor Statements
$users = LMongo::collection('users') ->where('votes', 100) ->norWhere('name', 'John') ->get();
Using Where In And Where Not In With An Array
$users = LMongo::collection('users') ->whereIn('id', array(1, 2, 3))->get();
Using Where All With An Array
$users = LMongo::collection('users') ->whereAll('tags', array('php','mongodb'))->get(); $users = LMongo::collection('users') ->whereNin('id', array(1, 2, 3))->get();
Using Where Exists
$users = LMongo::collection('users') ->whereExists('updated_at')->get();
Using Where Gt
$users = LMongo::collection('users') ->whereGt('votes', 1)->get();
Using Where Gte
$users = LMongo::collection('users') ->whereGte('votes', 1)->get();
Using Where Lt
$users = LMongo::collection('users') ->whereLt('votes', 1)->get();
Using Where Lte
$users = LMongo::collection('users') ->whereLte('votes', 1)->get();
Using Where Between
$users = LMongo::collection('users') ->whereBetween('votes', 1, 100)->get();
Using Where Ne
$users = LMongo::collection('users') ->whereNe('name', 'John')->get();
Using Where Regex
$users = LMongo::collection('users') ->whereRegex('name', '/John/i')->get(); //or $users = LMongo::collection('users') ->whereRegex('name', new MongoRegex('/John/im'))->get();
Using Where Like
$users = LMongo::collection('users') ->whereLike('name', 'John','im')->get();
There are more where methods in Query/Builder.php file.
Order By
$users = LMongo::collection('users') ->orderBy('name', 'desc') ->get(); $users = LMongo::collection('users') ->orderBy('name', -1) ->get();
Offset & Limit
$users = LMongo::collection('users')->skip(10)->take(5)->get();
Advanced Wheres
Parameter Grouping
LMongo::collection('users') ->where('name', 'John') ->orWhere(function($query) { $query->whereGt('votes', 100) ->whereNe('title', 'Admin'); }) ->get();
Aggregates
$users = LMongo::collection('users')->count(); $price = LMongo::collection('orders')->max('price'); $price = LMongo::collection('orders')->min('price'); $price = LMongo::collection('orders')->avg('price'); $total = LMongo::collection('users')->sum('votes');
Distinct
$emails = LMongo::collection('users')->distinct('email');
Inserts
Inserting Document Into A Collection
$id = LMongo::collection('users')->insert( array('email' => 'john@example.com', 'votes' => 0), );
Inserting Multiple Documents Into A Collection
$ids = LMongo::collection('users')->batchInsert( array('email' => 'john@example.com', 'votes' => 0), array('email' => 'henry@example.com', 'votes' => 0), );
Updates
Updating Documents In A Collection
LMongo::collection('users') ->where('id', 1) ->update(array('votes' => 1));
Incrementing or decrementing a value of a column
LMongo::collection('users')->increment('votes'); LMongo::collection('users')->decrement('votes');
Deletes
Deleting Documents In A Collection
LMongo::collection('users')->where('votes', 100)->delete(); //or LMongo::collection('users')->where('votes', 100)->remove();
Deleting All Documents From A Collection
LMongo::collection('users')->delete(); //or LMongo::collection('users')->truncate();
Pagination
LMongo has pagination support like Laravel's Query Builder.
$users = LMongo::collection('users')->orderBy('name')->paginate(10); foreach ($users as $user) { echo $user['name']; } echo $user->links();
Eloquent for MongoDB
It's similar to Eloquent, except few differences:
- It has a
collection
property, nottable
- Primary key field is
_id
, notid
- No Pivot collections for "Many To Many" relationship. So don't use "Many To Many" relationships on a large datasets.
See Eloquent Docs.
License
Licensed under the MIT License.
TODO
- Aggregate/group support
- Indexes
- GridFS