Add navruzm/lmongo as a requirement to composer.json:

  "require": {
      "navruzm/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:


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 navruzm/lmongo

This will copy the default configuration file to app/config/packages/navruzm/lmongo/config.php

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();

Retrieving A Single Document From A Collection with _id

$user = LMongo::collection('users')->find('1000000000000000000000');

You need convert your _id string to MongoID object, except find() method.

Retrieving All Rows From A Collection

$users = LMongo::collection('users')->get();

foreach ($users as $user)

Retrieving A Single Document From A Collection

$user = LMongo::collection('users')->where('name', 'John')->first();


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')

Nor Statements

$users = LMongo::collection('users')
              ->where('votes', 100)
              ->norWhere('name', 'John')

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')

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();
$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')

$users = LMongo::collection('users')
              ->orderBy('name', -1)

Offset & Limit

$users = LMongo::collection('users')->skip(10)->take(5)->get();

Parameter Grouping

          ->where('name', 'John')
              $query->whereGt('votes', 100)
                    ->whereNe('title', 'Admin');
$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');
$emails = LMongo::collection('users')->distinct('email');

Inserting Document Into A Collection

$id = LMongo::collection('users')->insert(
  array('email' => '', 'votes' => 0),

Inserting Multiple Documents Into A Collection

$ids = LMongo::collection('users')->batchInsert(
          array('email' => '', 'votes' => 0),
          array('email' => '', 'votes' => 0),

Updating Documents In A Collection

          ->where('id', 1)
          ->update(array('votes' => 1));

Incrementing or decrementing a value of a column



Deleting Documents In A Collection

LMongo::collection('users')->where('votes', 100)->delete();
LMongo::collection('users')->where('votes', 100)->remove();

Deleting All Documents From A Collection


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();

Most part of EloquentMongo is based on Illuminate/Database. It's similar to Laravel 4's 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. Don't use "Many To Many" relationships on a large datasets *
  • has() method not exists

See Eloquent Docs.

* Many to many relations require usually double the amount of hits to the database to keep both sides of the relation in sync, since keys are stored on both sides. Due to this they are slower and should be used with caution.