PHPackages                             suntekcorps/hyperf-mongodb - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. suntekcorps/hyperf-mongodb

ActiveLibrary[Database &amp; ORM](/categories/database)

suntekcorps/hyperf-mongodb
==========================

suntekcorps

v2.2.0(3y ago)1534↓100%2PHPPHP &gt;=7.2

Since Oct 9Pushed 3y ago1 watchersCompare

[ Source](https://github.com/st-tree/hyperf-mongodb)[ Packagist](https://packagist.org/packages/suntekcorps/hyperf-mongodb)[ RSS](/packages/suntekcorps-hyperf-mongodb/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (16)Used By (0)

hyperf mongodb pool
===================

[](#hyperf-mongodb-pool)

```
composer require suntekcorps/hyperf-mongodb

```

config
------

[](#config)

在/config/autoload目录里面创建文件 mongodb.php 添加以下内容

```
return [
   'default' => [
       'mode' => 0,
       'settings' => [
           0 => [
               'host' => env('MONGODB_HOST', '127.0.0.1'),
               'port' => env('MONGODB_PORT', 27017),
               'username' => env('MONGODB_USERNAME', ''),
               'password' => env('MONGODB_PASSWORD', ''),
               'db' => env('MONGODB_DB', 'test'),
               //设置复制集,没有不设置
               //'replica' => 'rs0',
               //设置读偏好,没有不设置
               //'readPreference' => 'secondaryPreferred'
           ],
           1 => [
               'host' => [
                   env('MONGODB_HOST_1', '127.0.0.1'),
                   env('MONGODB_HOST_2', '127.0.0.1'),
               ],
               'port' => [
                   env('MONGODB_PORT_1', 27017),
                   env('MONGODB_PORT_2', 27017),
               ],
               'username' => env('MONGODB_USERNAME', ''),
               'password' => env('MONGODB_PASSWORD', ''),
               'db' => env('MONGODB_DB', 'test'),
               //设置复制集,没有不设置
               //'replica' => 'rs0',
               //设置读偏好,没有不设置
               //'readPreference' => 'secondaryPreferred'
           ],
       ],
       'authMechanism' => 'SCRAM-SHA-256',
       '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 $mongoDbClient;
```

#### **tips:**

[](#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);
```

### 分页查询

[](#分页查询)

```
$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统计

[](#count统计)

```
$filter = ['isGroup' => "0", 'wechat' => '15584044700'];
$count = $this->$mongoDbClient->count('fans', $filter);
```

### Command，执行更复杂的mongo命令

[](#command执行更复杂的mongo命令)

**sql** 和 **mongodb** 关系对比图

SQLMongoDbWHERE$match (match里面可以用and，or，以及逻辑判断，但是好像不能用where)GROUP BY$groupHAVING$matchSELECT$projectORDER BY$sortLIMIT$limitSUM()$sumCOUNT()$sum```
$pipeline= [
            [
                '$match' => $where
            ], [
                '$group' => [
                    '_id' => [],
                    'groupCount' => [
                        '$sum' => '$groupCount'
                    ]
                ]
            ], [
                '$project' => [
                    'groupCount' => '$groupCount',
                    '_id' => 0
                ]
            ]
];

$count = $this->$mongoDbClient->command('fans', $pipeline);
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~52 days

Recently: every ~133 days

Total

14

Last Release

1353d ago

Major Versions

v0.1.1 → 2.1.x-dev2021-05-10

v0.1.3 → v2.2.02022-08-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/d64da9c8c4f12d3140a44879f412616b0f4409d668d6bfcf04bf76152538236a?d=identicon)[zjhuang@suntekcorps.com](/maintainers/zjhuang@suntekcorps.com)

---

Top Contributors

[![Erugihs](https://avatars.githubusercontent.com/u/71120455?v=4)](https://github.com/Erugihs "Erugihs (18 commits)")[![st-tree](https://avatars.githubusercontent.com/u/52597094?v=4)](https://github.com/st-tree "st-tree (2 commits)")

### Embed Badge

![Health badge](/badges/suntekcorps-hyperf-mongodb/health.svg)

```
[![Health](https://phpackages.com/badges/suntekcorps-hyperf-mongodb/health.svg)](https://phpackages.com/packages/suntekcorps-hyperf-mongodb)
```

###  Alternatives

[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k46.2M402](/packages/robmorgan-phinx)[kimai/kimai

Kimai - Time Tracking

4.6k7.4k1](/packages/kimai-kimai)[hyperf/database

A flexible database library.

202.8M257](/packages/hyperf-database)[yumufeng/hyperf-mongodb

2116.0k](/packages/yumufeng-hyperf-mongodb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
