PHP code example of reasno / fastmongo
1. Go to this page and download the library: Download reasno/fastmongo 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' );
reasno / fastmongo example snippets
composer
namespace App \Controller ;
use Hyperf \GoTask \MongoClient \MongoClient ;
class IndexController
{
public function index (MongoClient $client)
{
$col = $client->my_database->my_col;
$col->insertOne(['gender' => 'male' , 'age' => 18 ]);
$col->insertMany([['gender' => 'male' , 'age' => 20 ], ['gender' => 'female' , 'age' => 18 ]]);
$col->countDocuments();
$col->findOne(['gender' => 'male' ]);
$col->find(['gender' => 'male' ], ['skip' => 1 , 'limit' => 1 ]);
$col->updateOne(['gender' => 'male' ], ['$inc' => ['age' => 1 ]]);
$col->updateMany(['gender' => 'male' ], ['$inc' => ['age' => 1 ]]);
$col->replaceOne(['gender' => 'female' ], ['gender' => 'female' , 'age' => 15 ]);
$col->aggregate([
['$match' => ['gender' => 'male' ]],
['$group' => ['_id' => '$gender' , 'total' => ['$sum' => '$age' ]]],
]);
$col->deleteOne(['gender' => 'male' ]);
$col->deleteMany(['age' => 15 ]);
$col->drop();
$client->my_database->runCommand(['ping' => 1 ]);
return $client->my_database->runCommandCursor(['listCollections' => 1 ]);
}
}