This commit is contained in:
chenyao 2020-07-24 15:21:49 +08:00
parent 884c564dac
commit f51a97b71f

171
README.md
View File

@ -1,37 +1,154 @@
# hyperf-mongodb
#### 介绍
基于hyperf的mongodb连接池组件暂不支持协程。基于项目yumufenghyperf-mongodb做了个性化修改
#### 软件架构
软件架构说明
基于hyperf的mongodb连接池组件暂不支持协程。基于项目yumufenghyperf-mongodb做了个性化的优化。
#### 安装教程
# hyperf mongodb pool
1. xxxx
2. xxxx
3. xxxx
```
composer require adamchen1208/hyperf-mongodb
```
#### 使用说明
1. xxxx
2. xxxx
3. xxxx
#### 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
## config
在/config/autoload目录里面创建文件 mongodb.php
添加以下内容
```php
return [
'default' => [
'username' => env('MONGODB_USERNAME', ''),
'password' => env('MONGODB_PASSWORD', ''),
'host' => env('MONGODB_HOST', '127.0.0.1'),
'port' => env('MONGODB_PORT', 27017),
'db' => env('MONGODB_DB', 'test'),
'authMechanism' => 'SCRAM-SHA-256',
//设置复制集,没有不设置
//'replica' => 'rs0',
'pool' => [
'min_connections' => 3,
'max_connections' => 1000,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('MONGODB_MAX_IDLE_TIME', 60),
],
],
];
```
#### 码云特技
# 使用案例
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com)
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目
4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
使用注解,自动加载
**\Hyperf\Mongodb\MongoDb**
```php
/**
* @Inject()
* @var MongoDb
*/
protected $mongoDbClient;
```
#### **tips:**
查询的值是严格区分类型string、int类型的哦
### 新增
单个添加
```php
$insert = [
'account' => '',
'password' => ''
];
$this->$mongoDbClient->insert('fans',$insert);
```
批量添加
```php
$insert = [
[
'account' => '',
'password' => ''
],
[
'account' => '',
'password' => ''
]
];
$this->$mongoDbClient->insertAll('fans',$insert);
```
### 查询
```php
$where = ['account'=>'1112313423'];
$result = $this->$mongoDbClient->fetchAll('fans', $where);
```
### 分页查询
```php
$list = $this->$mongoDbClient->fetchPagination('article', 10, 0, ['author' => $author]);
```
### 更新
```php
$where = ['account'=>'1112313423'];
$updateData = [];
$this->$mongoDbClient->updateColumn('fans', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段
$this->$mongoDbClient->updateRow('fans',$where,$updateData);// 更新数据满足$where的行的信息成$newObject
```
### 删除
```php
$where = ['account'=>'1112313423'];
$all = true; // 为false只删除匹配的一条true删除多条
$this->$mongoDbClient->delete('fans',$where,$all);
```
### count统计
```php
$filter = ['isGroup' => "0", 'wechat' => '15584044700'];
$count = $this->$mongoDbClient->count('fans', $filter);
```
### Command执行更复杂的mongo命令
**sql** 和 **mongodb** 关系对比图
| SQL | MongoDb |
| --- | --- |
| WHERE | $match (match里面可以用andor以及逻辑判断但是好像不能用where) |
| GROUP BY | $group |
| HAVING | $match |
| SELECT | $project |
| ORDER BY | $sort |
| LIMIT | $limit |
| SUM() | $sum |
| COUNT() | $sum |
```php
$pipeline= [
[
'$match' => $where
], [
'$group' => [
'_id' => [],
'groupCount' => [
'$sum' => '$groupCount'
]
]
], [
'$project' => [
'groupCount' => '$groupCount',
'_id' => 0
]
]
];
$count = $this->$mongoDbClient->command('fans', $pipeline);
```