PHP code example of shershams / lmongo

1. Go to this page and download the library: Download shershams/lmongo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shershams / lmongo example snippets


'LMongo\LMongoServiceProvider'

'LMongo'          => 'LMongo\Facades\LMongo',
'EloquentMongo'   => 'LMongo\Eloquent\Model',

$LMongo = LMongo::connection();

$LMongo = LMongo::connection('othermongodbserver');

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

$mongodb = $LMongo->getMongoDB();

$collection_names = $mongodb->getCollectionNames();

$mongo = $LMongo->getMongoClient();

$databases = $mongo->listDBs();

$LMongo->selectDB($dbName);

$LMongo->createDB($dbName);

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

foreach ($users as $user)
{
    var_dump($user['name']);
}

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

var_dump($user['name']);

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

$users = LMongo::collection('users')->get(array('name', 'email'));

$users = LMongo::collection('users')->where('votes', 100)->get();

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

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

$users = LMongo::collection('users')
                ->whereIn('id', array(1, 2, 3))->get();

$users = LMongo::collection('users')
                ->whereAll('tags', array('php','mongodb'))->get();

$users = LMongo::collection('users')
                ->whereNin('id', array(1, 2, 3))->get();

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

$users = LMongo::collection('users')
                ->whereGt('votes', 1)->get();

$users = LMongo::collection('users')
                ->whereGte('votes', 1)->get();

$users = LMongo::collection('users')
                ->whereLt('votes', 1)->get();

$users = LMongo::collection('users')
                ->whereLte('votes', 1)->get();

$users = LMongo::collection('users')
                ->whereBetween('votes', 1, 100)->get();

$users = LMongo::collection('users')
                ->whereNe('name', 'John')->get();

$users = LMongo::collection('users')
                ->whereRegex('name', '/John/i')->get();
//or
$users = LMongo::collection('users')
                ->whereRegex('name', new MongoRegex('/John/im'))->get();

$users = LMongo::collection('users')
                ->whereLike('name', 'John','im')->get();

$users = LMongo::collection('users')
                ->orderBy('name', 'desc')
                ->get();

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

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

LMongo::collection('users')
            ->where('name', 'John')
            ->orWhere(function($query)
            {
                $query->whereGt('votes', 100)
                      ->whereNe('title', 'Admin');
            })
            ->get();

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

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

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

LMongo::collection('users')
            ->where('id', 1)
            ->update(array('votes' => 1));

LMongo::collection('users')->increment('votes');

LMongo::collection('users')->decrement('votes');

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

LMongo::collection('users')->delete();
//or
LMongo::collection('users')->truncate();

$users = LMongo::collection('users')->orderBy('name')->paginate(10);

foreach ($users as $user)
{
    echo $user['name'];
}

echo $user->links();
terminal
$ php artisan config:publish shershams/lmongo