1 Star 6 Fork 1

尧哥Adam / hyperf-mongodb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

hyperf-mongodb

介绍

用于hyperf的mongodb连接池组件,暂不支持协程。基于composer项目yumufeng/hyperf-mongodb和项目mongodb/mongodb做了个性化的优化。

hyperf mongodb pool

composer require adamchen1208/hyperf-mongodb

config

在/config/autoload目录里面创建文件 mongodb.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),
            ],
    ],
];

使用案例

使用注解,自动加载 \Hyperf\Mongodb\Mongodb

/**
 * @Inject()
 * @var Mongodb
*/
 protected $mongodb;

tips:

查询的值,是严格区分类型,string、int类型的哦

查询一条数据

$where = ['_id' => '1'];
$result = $this->$mongodb->findOne('test', $where);

查询全部数据

$where = ['_id' => '1'];
$result = $this->$mongodb->findAll('test', $where);

分页查询

$list = $this->$mongodb->findPagination('article', 10, 0, ['author' => $author]);

查询一条数据(_id自动转对象)

$where = ['_id' => '1'];
$result = $this->$mongodb->findOneId('test', $where);

查询全部数据(_id自动转对象)

$where = ['_id' => '1'];
$result = $this->$mongodb->findAllId('test', $where);

分页查询(_id自动转对象)

$list = $this->$mongodb->findPaginationId('article', 10, 0, ['author' => $author]);

插入一条数据

$insert = [
            '_id' => '',
            'password' => ''
];
$this->$mongodb->insert('test',$insert);

插入批量数据

$insert = [
            [
                '_id' => '',
                'password' => ''
            ],
            [
                '_id' => '',
                'password' => ''
            ]
];
$this->$mongodb->insertAll('test',$insert);

更新

$where = ['_id'=>'1112313423'];
$updateData = [];

$this->$mongodb->updateColumn('test', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段
$this->$mongodb->updateRow('test',$where,$updateData);// 更新数据满足$where的行的信息成$newObject

更新(_id自动转对象)

$where = ['_id'=>'1112313423'];
$updateData = [];

$this->$mongodb->updateColumnId('test', $where,$updateData); // 只更新数据满足$where的行的列信息中在$newObject中出现过的字段
$this->$mongodb->updateRowId('test',$where,$updateData);// 更新数据满足$where的行的信息成$newObject

删除

$where = ['_id'=>'1112313423'];
$all = true; // 为false只删除匹配的一条,true删除多条
$this->$mongodb->deleteOne('test',$where,$all);

批量删除

$where = ['_id'=>'1112313423'];
$all = true; // 为false只删除匹配的一条,true删除多条
$this->$mongodb->deleteMany('test',$where,$all);

删除(_id自动转对象)

$where = ['_id'=>'1112313423'];
$all = true; // 为false只删除匹配的一条,true删除多条
$this->$mongodb->deleteOneId('test',$where,$all);

统计

$filter = ['isGroup' => "0", 'wechat' => '15584044700'];
$count = $this->$mongodb->count('test', $filter);

聚合查询

sqlmongodb 关系对比图

SQL MongoDb
WHERE $match (match里面可以用and,or,以及逻辑判断,但是好像不能用where)
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT() $sum

$pipeline= [
            [
                '$match' => $where
            ], [
                '$group' => [
                    '_id' => [],
                    'groupCount' => [
                        '$sum' => '$groupCount'
                    ]
                ]
            ], [
                '$project' => [
                    'groupCount' => '$groupCount',
                    '_id' => 0
                ]
            ]
];

$count = $this->$mongodb->command('test', $pipeline);
MIT License Copyright (c) 2020 Adam Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

用于hyperf的mongodb连接池组件,暂不支持协程。基于composer项目yumufeng/hyperf-mongodb和项目mongodb/mongodb做了个性化的优化。 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/adamchen1208/hyperf-mongodb.git
git@gitee.com:adamchen1208/hyperf-mongodb.git
adamchen1208
hyperf-mongodb
hyperf-mongodb
master

搜索帮助