PHP code example of zrone / hyperf-mongodb
1. Go to this page and download the library: Download zrone/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/ */
zrone / hyperf-mongodb example snippets
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 Zrone\MongoDb\Example\Migrations;
use Zrone\MongoDb\MongoDbMigration;
class CreateTestCollection extends MongoDbMigration
{
/**
* 支持很多方法,请详细去看MongoDbMigration这个类
* @throws \Zrone\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 \Zrone\MongoDb\Exception\MongoDBException
*/
public function down()
{
return 'error';
}
}
php bin/hyperf.php mongodb:migrate
declare(strict_types=1);
namespace TmgAddons\WebQySession\Admin\Mongo;
use Zrone\MongoDb\MongoDb;
class TestMongo extends MongoDb
{
/**
* mongodb表
* @var null
*/
public $collectionName = 'co1';
}