gioco-plus / hyperf-mongodb
Installs: 21 695
Dependents: 2
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=7.2
- hyperf/pool: >2.0
- hyperf/utils: >2.0.0
- psr/container: >1.0
This package is auto-updated.
Last update: 2024-12-21 16:31:11 UTC
README
composer require gioco-plus/hyperf-mongodb
php bin/hyperf.php vendor:publish "gioco-plus/hyperf-mongodb"
動態切換連結池
/** * @Inject * @var ConfigInterface */ protected $config; /** * @Inject() * @var MongoDb */ protected $mongoDbClient; # 使用方式 $config = mongodb_pool_config('192.168.30.xx', 'ezadmin_yb', 27017, 'beta-db'); # 建立連結資訊 $this->config->set("mongodb.dbYB", $config); # 前綴 "mongodb." $this->mongoDbClient->setPool("dbYB")->insert("hyperf_test", [ 'aaa'=>'a', 'bbb'=>'b', 'ccc'=>'c' ]);
config
在/config/autoload目录里面创建文件 mongodb.php 添加以下内容
return [ 'default' => [ 'username' => env('MONGODB_USERNAME', ''), 'password' => env('MONGODB_PASSWORD', ''), 'host' => explode(';', env('MONGODB_HOST', '127.0.0.1')), 'port' => env('MONGODB_PORT', 27017), 'db' => env('MONGODB_DB', 'test'), 'options' => [ 'database' => 'admin', // 需要配置 username // 'authMechanism' => env('MONGODB_AuthMechanism', 'SCRAM-SHA-256'), //设置复制集,没有不设置 'replica' => env('MONGODB_Replica', 'rs0'), 'readPreference' => env('MONGODB_ReadPreference', 'primary'), ], '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), ], ], ];
使用案例
使用注解,自动加载 \GiocoPlus\Mongodb\MongoDb
/** * @Inject() * @var MongoDb */ protected $mongoDbClient;
tips:
查询的值,是严格区分类型,string、int类型的哦
新增
单个添加
$insert = [ 'account' => '', 'password' => '' ]; $this->mongoDbClient->insert('fans',$insert);
批量添加
$insert = [ [ 'account' => '', 'password' => '' ], [ 'account' => '', 'password' => '' ] ]; $this->mongoDbClient->insertAll('fans',$insert);
查询
$where = ['account'=>'1112313423']; $result = $this->mongoDbClient->fetchAll('fans', $where);
$result = $this->mongoDbClient->fetchAll('fans', [ 'id' => [ '$in' => ['a', 'b', 'c'] ] ], [ 'sort' => ['sort'=>1] ] ); # $or 的使用方式 $inputs['filter']['$or'] = [ [ '_id' => [ '$in' => $bull_ids ] ], [ 'for_all' => [ '$eq' => true ] ] ]; $result = $this->mongoDbClient->fetchAll('fans', $inputs['filter']);
分页查询
$list = $this->mongoDbClient->fetchPagination('article', 10, 0, ['author' => $author]);
更新
$where = ['account'=>'1112313423']; $updateData = []; $this->mongoDbClient->updateColumn('fans', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段 $this->mongoDbClient->updateRow('fans',$where,$updateData);// 更新数据满足$where的行的信息成$newObject
删除
$where = ['account'=>'1112313423']; $all = true; // 为false只删除匹配的一条,true删除多条 $this->mongoDbClient->delete('fans',$where,$all);
count统计
$filter = ['isGroup' => "0", 'wechat' => '15584044700']; $count = $this->mongoDbClient->count('fans', $filter);
Command,执行更复杂的mongo命令
sql 和 mongodb 关系对比图
$pipeline= [ [ '$match' => $where ], [ '$group' => [ '_id' => [], 'groupCount' => [ '$sum' => '$groupCount' ] ] ], [ '$project' => [ 'groupCount' => '$groupCount', '_id' => 0 ] ] ]; $count = $this->mongoDbClient->command('fans', $pipeline);