PHP code example of lmz / hyperf-mongodb
1. Go to this page and download the library: Download lmz/hyperf-mongodb 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' );
lmz / hyperf-mongodb example snippets
return [
'default' => [
'host' => [
'127.0.0.1' ,
],
'port' => 27017 ,
'database' => 'test' ,
'username' => 'user_dev' ,
'password' => '123456' ,
'options' => [
'database' => 'admin' ,
],
'pool' => [
'min_connections' => 10 ,
'max_connections' => 100 ,
'connect_timeout' => 10.0 ,
'wait_timeout' => 3.0 ,
'heartbeat' => -1 ,
'max_idle_time' => (float)env('DB_MAX_IDLE_TIME' , 60 ),
],
],
];
$container->get(MongoDB\Client::class)->selectDatabase('database_name' )->selectCollection('collection_name' );
class Course extends Mongodb
{
protected $mongoDbName = 'database_name' ;
protected $poolName = 'course' ;
public function db ()
{
$container = ApplicationContext::getContainer();
$mongodb = $container->get(self ::class);
return $mongodb->selectDatabase($this ->mongoDbName);
}
}
$container->get(Course::class)->db()->selectCollection('collection_name' )->findOne([
'id' => 1
])
$container->get(Hyperf\Mongodb\MongodbFactory::class)->get('course' )->selectDatabase('database_name' )->selectCollection('collection_name' )->findOne([
'id' => 1 ,
])