2020-07-24 07:08:38 +00:00
|
|
|
|
# hyperf-mongodb
|
|
|
|
|
|
|
|
|
|
#### 介绍
|
|
|
|
|
|
2022-10-23 09:16:42 +00:00
|
|
|
|
用于hyperf的mongodb连接池组件,暂不支持协程。基于 (
|
|
|
|
|
Adam/hyperf-mongodb)[https://gitee.com/adamchen1208/hyperf-mongodb?_from=gitee_search],
|
2021-09-22 10:22:27 +00:00
|
|
|
|
改作者好像已经不更新了,且源代码有些bug,依赖也存在一些问题。
|
2022-10-23 09:16:42 +00:00
|
|
|
|
|
2021-09-22 10:22:27 +00:00
|
|
|
|
1. 为此我clone该项目进行大幅度的修改,和原先项目变动较大,所以另起项目进行开源!
|
|
|
|
|
2. 增加单元测试,优化依赖。
|
|
|
|
|
3. 规范代码编写,简洁了源代码中一些不规范,重复高的代码。
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2022-10-23 07:22:06 +00:00
|
|
|
|
# 使用
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2022-10-23 07:22:06 +00:00
|
|
|
|
## 设置仓库源
|
2022-10-23 09:16:42 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
2022-10-23 07:22:06 +00:00
|
|
|
|
|
2021-09-22 10:22:27 +00:00
|
|
|
|
{
|
|
|
|
|
"repositories": [{
|
|
|
|
|
"type": "composer",
|
|
|
|
|
"url": "http://composer.zyimm.com"
|
|
|
|
|
}]
|
|
|
|
|
}
|
2022-10-23 07:22:06 +00:00
|
|
|
|
```
|
2022-10-23 09:16:42 +00:00
|
|
|
|
|
2022-10-23 07:22:06 +00:00
|
|
|
|
## 安装
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2022-10-23 07:22:06 +00:00
|
|
|
|
```
|
2021-09-22 10:22:27 +00:00
|
|
|
|
composer require zyimm/hyperf-mongodb
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2021-09-22 10:22:27 +00:00
|
|
|
|
## config
|
|
|
|
|
|
|
|
|
|
在/config/autoload目录里面创建文件 mongodb.php 添加以下内容
|
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```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),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
```
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
# 使用案例
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2021-09-22 10:22:27 +00:00
|
|
|
|
使用注解,自动加载
|
|
|
|
|
**\Hyperf\Mongodb\Mongodb**
|
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```php
|
2022-10-23 09:16:42 +00:00
|
|
|
|
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
2020-07-24 07:21:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* @Inject()
|
2020-07-26 14:39:23 +00:00
|
|
|
|
* @var Mongodb
|
2020-07-24 07:21:49 +00:00
|
|
|
|
*/
|
2022-10-23 09:16:42 +00:00
|
|
|
|
|
2020-07-26 14:39:23 +00:00
|
|
|
|
protected $mongodb;
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2021-09-22 10:22:27 +00:00
|
|
|
|
#### **tips:**
|
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
查询的值,是严格区分类型,string、int类型的哦
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2020-07-26 14:39:23 +00:00
|
|
|
|
### 查询一条数据
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$where = ['_id' => '1'];
|
|
|
|
|
$result = $this->$mongodb->findOne('test', $where);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 查询全部数据
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$where = ['_id' => '1'];
|
|
|
|
|
$result = $this->$mongodb->findAll('test', $where);
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-28 02:53:39 +00:00
|
|
|
|
### 分页查询
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-28 02:53:39 +00:00
|
|
|
|
```php
|
|
|
|
|
$list = $this->$mongodb->findPagination('article', 10, 0, ['author' => $author]);
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-26 14:39:23 +00:00
|
|
|
|
### 查询一条数据(_id自动转对象)
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$where = ['_id' => '1'];
|
2020-07-28 06:38:55 +00:00
|
|
|
|
$result = $this->$mongodb->findOneId('test', $where);
|
2020-07-26 14:39:23 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 查询全部数据(_id自动转对象)
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$where = ['_id' => '1'];
|
2020-07-28 06:38:55 +00:00
|
|
|
|
$result = $this->$mongodb->findAllId('test', $where);
|
2020-07-26 14:39:23 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-07-28 02:53:39 +00:00
|
|
|
|
### 分页查询(_id自动转对象)
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-26 14:39:23 +00:00
|
|
|
|
```php
|
2020-07-28 06:38:55 +00:00
|
|
|
|
$list = $this->$mongodb->findPaginationId('article', 10, 0, ['author' => $author]);
|
2020-07-26 14:39:23 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-07-28 02:53:39 +00:00
|
|
|
|
### 插入一条数据
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```php
|
|
|
|
|
$insert = [
|
2020-07-26 14:39:23 +00:00
|
|
|
|
'_id' => '',
|
2020-07-24 07:21:49 +00:00
|
|
|
|
'password' => ''
|
|
|
|
|
];
|
2020-07-26 14:39:23 +00:00
|
|
|
|
$this->$mongodb->insert('test',$insert);
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
2020-07-24 07:08:38 +00:00
|
|
|
|
|
2020-07-28 02:53:39 +00:00
|
|
|
|
### 插入批量数据
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```php
|
|
|
|
|
$insert = [
|
|
|
|
|
[
|
2020-07-26 14:39:23 +00:00
|
|
|
|
'_id' => '',
|
2020-07-24 07:21:49 +00:00
|
|
|
|
'password' => ''
|
|
|
|
|
],
|
|
|
|
|
[
|
2020-07-26 14:39:23 +00:00
|
|
|
|
'_id' => '',
|
2020-07-24 07:21:49 +00:00
|
|
|
|
'password' => ''
|
|
|
|
|
]
|
|
|
|
|
];
|
2020-07-26 14:39:23 +00:00
|
|
|
|
$this->$mongodb->insertAll('test',$insert);
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 更新
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```php
|
2020-07-26 14:39:23 +00:00
|
|
|
|
$where = ['_id'=>'1112313423'];
|
2020-07-24 07:21:49 +00:00
|
|
|
|
$updateData = [];
|
|
|
|
|
|
2020-07-26 14:39:23 +00:00
|
|
|
|
$this->$mongodb->updateColumn('test', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段
|
|
|
|
|
$this->$mongodb->updateRow('test',$where,$updateData);// 更新数据满足$where的行的信息成$newObject
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
2020-07-28 06:38:55 +00:00
|
|
|
|
|
|
|
|
|
### 更新(_id自动转对象)
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-28 06:38:55 +00:00
|
|
|
|
```php
|
|
|
|
|
$where = ['_id'=>'1112313423'];
|
|
|
|
|
$updateData = [];
|
|
|
|
|
|
|
|
|
|
$this->$mongodb->updateColumnId('test', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段
|
|
|
|
|
$this->$mongodb->updateRowId('test',$where,$updateData);// 更新数据满足$where的行的信息成$newObject
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
### 删除
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```php
|
2020-07-26 14:39:23 +00:00
|
|
|
|
$where = ['_id'=>'1112313423'];
|
2020-07-24 07:21:49 +00:00
|
|
|
|
$all = true; // 为false只删除匹配的一条,true删除多条
|
2020-07-28 06:38:55 +00:00
|
|
|
|
$this->$mongodb->deleteOne('test',$where,$all);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 批量删除
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-28 06:38:55 +00:00
|
|
|
|
```php
|
|
|
|
|
$where = ['_id'=>'1112313423'];
|
|
|
|
|
$all = true; // 为false只删除匹配的一条,true删除多条
|
|
|
|
|
$this->$mongodb->deleteMany('test',$where,$all);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 删除(_id自动转对象)
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-28 06:38:55 +00:00
|
|
|
|
```php
|
|
|
|
|
$where = ['_id'=>'1112313423'];
|
|
|
|
|
$all = true; // 为false只删除匹配的一条,true删除多条
|
|
|
|
|
$this->$mongodb->deleteOneId('test',$where,$all);
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-07-28 02:53:39 +00:00
|
|
|
|
### 统计
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```php
|
|
|
|
|
$filter = ['isGroup' => "0", 'wechat' => '15584044700'];
|
2020-07-26 14:39:23 +00:00
|
|
|
|
$count = $this->$mongodb->count('test', $filter);
|
2020-07-24 07:21:49 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-07-28 02:53:39 +00:00
|
|
|
|
### 聚合查询
|
2021-09-22 10:22:27 +00:00
|
|
|
|
|
2020-07-24 07:21:49 +00:00
|
|
|
|
**sql** 和 **mongodb** 关系对比图
|
|
|
|
|
|
|
|
|
|
| SQL | MongoDb |
|
|
|
|
|
| --- | --- |
|
|
|
|
|
| WHERE | $match (match里面可以用and,or,以及逻辑判断,但是好像不能用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
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2020-07-26 14:39:23 +00:00
|
|
|
|
$count = $this->$mongodb->command('test', $pipeline);
|
2020-07-28 02:53:39 +00:00
|
|
|
|
```
|