jufubao/fastmongo

通过 hyperf/gotask 实现的 mongodb 驱动

维护者

详细信息

gitee.com/jufubao/fastmongo.git

1.0.1 2022-10-14 07:05 UTC

This package is not auto-updated.

Last update: 2024-09-18 16:36:01 UTC


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。