reasno/fastmongo
mongodb 驱动程序,通过 hyperf/gotask 实现
v0.2.6
2022-02-21 02:18 UTC
Requires
- php: >=7.2
- ext-mongodb: *
- ext-swoole: >=4.4
- hyperf/gotask: ^2.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- hyperf/testing: 1.1.*
- phpstan/phpstan: ^0.10.5
- swoole/ide-helper: ^4.5
README
reasno/fastmongo
是一个基于协程的 MongoDB 客户端,用于 Hyperf,由 hyperf/gotask
驱动。
安装
composer require reasno/fastmongo
- 需要 MongoDB 扩展。
- MongoDB > 4.0
- 不需要 MongoDB PHP 库。
- 不需要 Go。
配置
只需设置环境变量 MONGODB_URI。(默认为 mongodb://127.0.0.1:27017
)
您还可以使用 php bin/hyperf.php vendor:publish reasno/fastmongo
导出配置。
API 列表
<?php namespace App\Controller; use Hyperf\GoTask\MongoClient\MongoClient; class IndexController { public function index(MongoClient $client) { $col = $client->my_database->my_col; $col->insertOne(['gender' => 'male', 'age' => 18]); $col->insertMany([['gender' => 'male', 'age' => 20], ['gender' => 'female', 'age' => 18]]); $col->countDocuments(); $col->findOne(['gender' => 'male']); $col->find(['gender' => 'male'], ['skip' => 1, 'limit' => 1]); $col->updateOne(['gender' => 'male'], ['$inc' => ['age' => 1]]); $col->updateMany(['gender' => 'male'], ['$inc' => ['age' => 1]]); $col->replaceOne(['gender' => 'female'], ['gender' => 'female', 'age' => 15]); $col->aggregate([ ['$match' => ['gender' => 'male']], ['$group' => ['_id' => '$gender', 'total' => ['$sum' => '$age']]], ]); $col->deleteOne(['gender' => 'male']); $col->deleteMany(['age' => 15]); $col->drop(); // if there is a command not yet supported, use runCommand or runCommandCursor. $client->my_database->runCommand(['ping' => 1]); return $client->my_database->runCommandCursor(['listCollections' => 1]); } }
背景
此包利用 hyperf/gotask
实现协程。
在 hyperf/gotask
v2.1.0 中添加了一个新的 MongoDB 包。通常 GoTask 需要你在 Go 中编写一些代码。然而,这种方法需要一定的 Go 熟练度,这可能是一个障碍。《reasno/fastmongo》是新添加的 MongoDB 包的预构建版本。它为您提供了 Go 二进制文件,因此您不需要自己编译。
此包仅公开了一个非常简单但经过优化的配置接口。如果需要更多定制,请检查原始的 hyperf/gotask
。
请不要同时打开
hyperf/gotask
和此包。
未来范围
- 可以添加更多 MongoDB 命令。请随时创建问题或向
hyperf/gotask
提交您的 PR。