PHP code example of aheinze / mongo-lite

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

    

aheinze / mongo-lite example snippets

 php
$client     = new MongoLite\Client(PATH_TO_WRITABLE_FOLDER);
$database   = $client->testdb;
$collection = $database->products;

$entry = ["name"=>"Super cool Product", "price"=>20];

$collection->insert($entry);

$products = $collection->find(); // Get Cursor

if ($products->count()) {

    foreach($products->sort(["price"=>1])->limit(5) as $product) {
        var_dump($product);
    }
}
 php
$collection->find(function($document) {   // recommended to query data
    return $document["price"] > 10;
});

//or

$collection->find(["price"=>['$gt'=>10]]); // only very simple criteria is supported (can be slow)

//or just one

$collection->findOne(function($document) { ... });
$collection->findOne([...]);
 php
$collection->insert($document);
$collection->save($document);
$collection->update($criteria, $data);
 php
$collection->remove($criteria);
 php
Client::listDBs()
Client::selectDB(databasename)
Client::selectCollection(databasename, collectionname)
 php
Cursor::count()
Cursor::limit(number)
Cursor::skip(number)
Cursor::sort(array)
Cursor::each($callable)
Cursor::toArray()