PHP code example of phper666 / mongo-db

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

    

phper666 / mongo-db example snippets



declare(strict_types=1);
namespace TmgAddon\WebQySession\Model;

use Hyperf\Task\Annotation\Task;
use Phper666\MongoDb\MongoDb;
class Test extends MongoDb
{
    public $collectionName = 'test';

    public static function collectionName()
    {
        return 'test';
    }

    /**
     * @Task(timeout=20)
     * @return array
     */
    public function getMethod($method, ...$params)
    {
        return $this->{$method}(...$params);
    }
}

// 调用,实际跟原有的findOne调用的参数是一致的,只不过第一个参数时申明要调用申明方法
$data = make(Test::class)->getMethod('findOne', ['name' => '1'], ['projection' => ['name' => 1]]);


php bin/hyperf.php mongodb:publish --config


declare(strict_types=1);
return [
    'default' => [
        'uri_options' => [
            'ssl' => true,
            'username' => env('MONGODB_USERNAME', ''),
            'password' => env('MONGODB_PASSWORD', ''),
//            'authMechanism' => env('MONGODB_AUTH_MECHANISM', 'SCRAM-SHA-256'),
            //设置复制集,没有不设置
//        'replicaSet' => 'rs0',
        ],
        'host' => env('MONGODB_HOST', '127.0.0.1'),
        'port' => env('MONGODB_PORT', 27017),
        'db' => env('MONGODB_DB', 'test'),
        'driver_options' => [],
        'migration' => [
            'path' => BASE_PATH . '/migrations/mongodb', // 迁移文件的路径
        ],
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 100,
            'connect_timeout' => 10.0,
            'wait_timeout' => 3.0,
            'heartbeat' => -1,
            'max_idle_time' => (float)env('MONGODB_MAX_IDLE_TIME', 60),
        ],
    ],
];

php bin/hyperf.php mongodb:migration Test


declare(strict_types=1);
namespace Phper666\MongoDb\Example\Migrations;
use Phper666\MongoDb\MongoDbMigration;
class CreateTestCollection extends MongoDbMigration
{
    /**
     * 支持很多方法,请详细去看MongoDbMigration这个类
     * @throws \Phper666\MongoDb\Exception\MongoDBException
     */
    public function up()
    {
        $msg = [];
        $msg[] = $this->createCollection('test'); // 创建一个表
        $data = [
            ['dd' => 1, 'tt' => 2],
            ['dd' => 2, 'tt' => 4],
        ];
        $msg[] = $this->insertMany('test', $data); // 插入多条数据
        $msg[] = $this->createIndex('test', ['dd' => 1, 'tt' => 1]); // 在该表上创建索引
        $msg[] = $this->createIndexes('test', [['dd' => 1], ['tt' => 1]]); // 在该表上批量创建索引
        $msg[] = $this->dropCollection('test'); // 删除一个表
        return $msg;
    }

    /**
     * 迁移失败时会执行
     * @throws \Phper666\MongoDb\Exception\MongoDBException
     */
    public function down()
    {
        return 'error';
    }
}

php bin/hyperf.php mongodb:migrate 


declare(strict_types=1);
namespace TmgAddons\WebQySession\Admin\Mongo;

use Phper666\MongoDb\MongoDb;
class TestMongo extends MongoDb
{
    /**
     * mongodb表
     * @var null
     */
    public $collectionName = 'co1';
}