PHP code example of maer / mongo-query

1. Go to this page and download the library: Download maer/mongo-query 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/ */

    

maer / mongo-query example snippets


// Load composers autoloader
ents and a MongoDB\Client instance will be created
// (using localhost and default port)
$client = new Maer\MongoQuery\Connection();

// To use a custom client instance, pass it as the first argument
$client = $client = new Maer\MongoQuery\Connection(
    new MongoDB\Client('mongodb://example.com:1234')
);


// Get a database
$db = $client->myDatabase; // Or: $client->getDatabase('myDatabase');

// Get a collection
$collection = $db->myCollection; // Or: $db->getCollection('myCollection');

// Add some criterias
$result = $collection->where('some-key', 'some-value')
    ->orderBy('some-key', 'asc')
    ->get();

// $result will now contain an array with the matched documents

$id = $collection->insert([
    'some-key' => 'some-value',
]);

// $id contains the new id for the inserted document or false on error


$ids = $collection->insert([
    [
        'some-key' => 'some-value',
    ],
    [
        'some-key' => 'some-other-value',
    ]
]);

// $ids contains an array of the new ids for the inserted documents or false on error

$result = $collection->get();

$result = $collection->first();

// Find by _id (default)
$result = $collection->find('123');

// Find by some other key (is equal to)
$result = $collection->find('some-value', 'some-key');

$result = $collection->where('some-key', 'some-value')->count();
// $result is an integer with the number of matched documents

$result = $colletion->get();

$result = $collection->where('some-key', 'some-value')->get();

$result = $collection->where('some-key', '=', 'some-value')
    ->where('some-other-key', '!=', 'some-other-value')
    ...
    ->get();

$collection->orWhere('some-key', 'some-value')
    ->orWhere('some-other-key', 'some-other-value');

// Same as:
// [
//     '$or' => [
//         [
//             'some-key' => 'some-value',
//         ],
//         [
//             'some-other-key' => 'some-other-value'
//         ]
//     ],
// ]


$collection->orWhere(function ($query) {
    $query->where('some-key', 'some-value')
        ->where('some-other-key', 'some-other-value');
});


// Same as:
// [
//     '$or' => [
//         [
//             'some-key' => 'some-value',
//             'some-other-key' => 'some-other-value'
//         ]
//     ],
// ]

// Ascending order:
$result = $collection->orderBy(['first_name' => 'asc']);

// Descending order:
$result = $collection->orderBy(['first_name' => 'desc']);

// Only get the 2 first matches
$result = $collection->people->limit(2)->get();

// Get all results from the second match and forward.
$result = $collection->offset(2)->get();

// Define what fields you want to get
$result = $collection->select(['some-key', 'some-other-key'])->get();

$result = $collection->pluck('some-key');

// Returns
// [
//     'first-value',
//     'second-value',
//     ...
// ]

$modified = $collection->where('some-key', 'some-value')
    ...
    ->updateOne([
        'some-key' => 'some-new-value',
    ]);

$modified = $collection->where('some-key', 'some-value')
    ...
    ->updateMany([
        'some-key' => 'some-new-value',
    ]);

$modified = $collection->insert([
    'foo'     => 'Lorem',
    'bar'     => 'Ipsum',
]);

$modified = $collection->where('foo', 'Lorem')
    ->replaceOne([
        'foo_bar' => 'Lorem Ipsum',
    ]);

$result = $collection->first();
// Returns:
// [
//     '_id'      => xxx,
//     'foo_bar' => 'Lorem Ipsum',
// ]

$result = $collection->where('some-key', 'some-value')
    ->deleteOne();

$mongo = new Maer\MongoQuery\Connection;

// Get MongoDB\Client
$client = $mongo->getClient();

// Get MongoDB\Database
$database = $mongo->someDatabase->getDatabase();

// Get MongoDB\Collection
$collection = $mongo->someDatabase->someCollection->getCollection();