Code Monkey home page Code Monkey logo

fastmongo's Introduction

reasno/fastmongo is the a coroutine-based mongodb client for Hyperf, powered by hyperf/gotask.

Installation

composer require reasno/fastmongo
  • MongoDB extension is required.
  • MongoDB > 4.0
  • MongoDB PHP library is NOT required.
  • Go is NOT required.

Configuration

Just set the environmental variable MONGODB_URI. (Defaults to mongodb://127.0.0.1:27017)

You may also use php bin/hyperf.php vendor:publish reasno/fastmongo to export a configuration.

API List

<?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]); 
    }
}

Background

This package makes use of hyperf/gotask to achieve coroutine.

In hyperf/gotask v2.1.0 a new mongodb package is added. Normally GoTask requires you to do some coding in Go. However this approach demands some proficiency in Go, which can be prohibitive. reasno/fastmongo is a prebuilt version of the newly added mongodb package. It vendors the Go binary for you so you don't have to compile your own.

This package only exposes a very simple yet optimized configuration interface. Should more customization be needed, checkout out the original hyperf/gotask.

Please do not turn on hyperf/gotask and this package at the same time.

Future Scope

  • More mongodb commands can be added. Please feel free to create issues or submit your PRs to hyperf/gotask.

fastmongo's People

Contributors

jword avatar leocavalcante avatar reasno avatar

Stargazers

 avatar

Watchers

 avatar

fastmongo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.