LMongo
LMongo is MongoDB package for Laravel 4. Most part of LMongo is based on Illuminate/Database (Thanks to @taylorotwell), (*1)
Please note that LMongo project is under heavy development, some functionalities are not yet tested. Please report if you find any bug., (*2)
Installation
Add shershams/lmongo
as a requirement to composer.json:, (*3)
{
"require": {
"shershams/lmongo": "*"
}
}
And then run composer update
, (*4)
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:, (*5)
'LMongo\LMongoServiceProvider'
Then find the aliases
key and add following line to the array:, (*6)
'LMongo' => 'LMongo\Facades\LMongo',
'EloquentMongo' => 'LMongo\Eloquent\Model',
Finally you need to publish a configuration file by running the following Artisan command., (*7)
$ php artisan config:publish shershams/lmongo
This will copy the default configuration file to app/config/packages/shershams/lmongo/config.php, (*8)
Basic Usage
You may get a MongoDB instance by calling the LMongo::connection
method:, (*9)
$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:, (*10)
$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:, (*11)
$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:, (*12)
$mongodb = $LMongo->getMongoDB();
$collection_names = $mongodb->getCollectionNames();
Get the MongoClient object:, (*13)
$mongo = $LMongo->getMongoClient();
$databases = $mongo->listDBs();
Select/switch the database:, (*14)
$LMongo->selectDB($dbName);
Create the database:, (*15)
$LMongo->createDB($dbName);
Query Builder
Wheres
Retrieving All Rows From A Collection, (*16)
$users = LMongo::collection('users')->get();
foreach ($users as $user)
{
var_dump($user['name']);
}
Retrieving A Single Document From A Collection, (*17)
$user = LMongo::collection('users')->where('name', 'John')->first();
var_dump($user['name']);
Retrieving A Single Column From A Document, (*18)
$name = LMongo::collection('users')->where('name', 'John')->pluck('name');
Specifying A Fields, (*19)
$users = LMongo::collection('users')->get(array('name', 'email'));
Using Where Operators, (*20)
$users = LMongo::collection('users')->where('votes', 100)->get();
Or Statements, (*21)
$users = LMongo::collection('users')
->where('votes', 100)
->orWhere('name', 'John')
->get();
Nor Statements, (*22)
$users = LMongo::collection('users')
->where('votes', 100)
->norWhere('name', 'John')
->get();
Using Where In And Where Not In With An Array, (*23)
$users = LMongo::collection('users')
->whereIn('id', array(1, 2, 3))->get();
Using Where All With An Array, (*24)
$users = LMongo::collection('users')
->whereAll('tags', array('php','mongodb'))->get();
$users = LMongo::collection('users')
->whereNin('id', array(1, 2, 3))->get();
Using Where Exists, (*25)
$users = LMongo::collection('users')
->whereExists('updated_at')->get();
Using Where Gt, (*26)
$users = LMongo::collection('users')
->whereGt('votes', 1)->get();
Using Where Gte, (*27)
$users = LMongo::collection('users')
->whereGte('votes', 1)->get();
Using Where Lt, (*28)
$users = LMongo::collection('users')
->whereLt('votes', 1)->get();
Using Where Lte, (*29)
$users = LMongo::collection('users')
->whereLte('votes', 1)->get();
Using Where Between, (*30)
$users = LMongo::collection('users')
->whereBetween('votes', 1, 100)->get();
Using Where Ne, (*31)
$users = LMongo::collection('users')
->whereNe('name', 'John')->get();
Using Where Regex, (*32)
$users = LMongo::collection('users')
->whereRegex('name', '/John/i')->get();
//or
$users = LMongo::collection('users')
->whereRegex('name', new MongoRegex('/John/im'))->get();
Using Where Like, (*33)
$users = LMongo::collection('users')
->whereLike('name', 'John','im')->get();
There are more where methods in Query/Builder.php file., (*34)
Order By, (*35)
$users = LMongo::collection('users')
->orderBy('name', 'desc')
->get();
$users = LMongo::collection('users')
->orderBy('name', -1)
->get();
Offset & Limit, (*36)
$users = LMongo::collection('users')->skip(10)->take(5)->get();
Advanced Wheres
Parameter Grouping, (*37)
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, (*38)
$id = LMongo::collection('users')->insert(
array('email' => 'john@example.com', 'votes' => 0),
);
Inserting Multiple Documents Into A Collection, (*39)
$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, (*40)
LMongo::collection('users')
->where('id', 1)
->update(array('votes' => 1));
Incrementing or decrementing a value of a column, (*41)
LMongo::collection('users')->increment('votes');
LMongo::collection('users')->decrement('votes');
Deletes
Deleting Documents In A Collection, (*42)
LMongo::collection('users')->where('votes', 100)->delete();
//or
LMongo::collection('users')->where('votes', 100)->remove();
Deleting All Documents From A Collection, (*43)
LMongo::collection('users')->delete();
//or
LMongo::collection('users')->truncate();
LMongo has pagination support like Laravel's Query Builder., (*44)
$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, not table
* Primary key field is _id
, not id
* No Pivot collections for "Many To Many" relationship. So don't use "Many To Many" relationships on a large datasets., (*45)
See Eloquent Docs., (*46)
License
Licensed under the MIT License., (*47)
TODO
- Aggregate/group support
- Indexes
- GridFS