PHPackages                             kengxin/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. kengxin/hyperf-mongodb

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

kengxin/hyperf-mongodb
======================

03.4k↑183.5%PHP

Since Feb 23Pushed 3y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#hyperf-mongodb-pool)

```
composer require yumufeng/hyperf-mongodb

```

config
------

[](#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 $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

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

 Bus Factor1

Top contributor holds 83.3% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/738b03b12932fbd3565067e9dee850e0e3cc151fa9333f50facf1f76265d25a0?d=identicon)[kengxin](/maintainers/kengxin)

---

Top Contributors

[![yumufeng](https://avatars.githubusercontent.com/u/9146937?v=4)](https://github.com/yumufeng "yumufeng (10 commits)")[![kengxin](https://avatars.githubusercontent.com/u/18631708?v=4)](https://github.com/kengxin "kengxin (2 commits)")

### Embed Badge

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

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M545](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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