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

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

adamchen1208/hyperf-mongodb
===========================

2.1.3(5y ago)048[1 issues](https://github.com/adamchen1208/hyperf-mongodb/issues)MITPHPPHP &gt;=7.2

Since Jul 24Pushed 5y agoCompare

[ Source](https://github.com/adamchen1208/hyperf-mongodb)[ Packagist](https://packagist.org/packages/adamchen1208/hyperf-mongodb)[ RSS](/packages/adamchen1208-hyperf-mongodb/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (3)Versions (5)Used By (0)

hyperf-mongodb
==============

[](#hyperf-mongodb)

#### 介绍

[](#介绍)

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

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

[](#hyperf-mongodb-pool)

```
composer require adamchen1208/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 $mongodb;
```

#### **tips:**

[](#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自动转对象）

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

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

### 查询全部数据（\_id自动转对象）

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

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

### 分页查询（\_id自动转对象）

[](#分页查询_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自动转对象）

[](#更新_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自动转对象）

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

### 聚合查询

[](#聚合查询)

**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->$mongodb->command('test', $pipeline);
```

###  Health Score

24

↑

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~1 days

Total

4

Last Release

2113d ago

Major Versions

1.1.0 → 2.1.12020-07-26

### Community

Maintainers

![](https://www.gravatar.com/avatar/235ac12bec64ea2fcaa958310e96b2edd0491353082220f41b465accce2bbd0a?d=identicon)[adamchen1208](/maintainers/adamchen1208)

---

Top Contributors

[![adamchen1208](https://avatars.githubusercontent.com/u/16108113?v=4)](https://github.com/adamchen1208 "adamchen1208 (16 commits)")

---

Tags

hyperfhyperf-mongodbmognodb

### Embed Badge

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

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

###  Alternatives

[robmorgan/phinx

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

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

Kimai - Time Tracking

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

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

PHPackages © 2026

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