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

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

    

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,// 90
            '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
 * @return Client|Course
 * @package App\Model\Mongo
 */
class Course extends Mongodb
{

    protected $mongoDbName = 'database_name';    // mongoDB 表名

    protected $poolName = 'course';// 连接池名称

    public function db()
    {
        $container = ApplicationContext::getContainer();
        /**
         * @var $mongodb Client
         */
        $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,
])