GithubHelp home page GithubHelp logo

asyncmongo's Introduction

Usage

Connect to server

use Echore\AsyncMongo\AsyncMongoDB;
use pocketmine\Server;

/**
 * @var Server $pmmpServer
 */

$mongo = new AsyncMongoDB(
    $pmmpServer,
    $poolLimit, // max count of threads
    null, // server uri, nullable (uses default uri)
    [], // uri options,
    [] // mongodb driver options
);

Close threads

/**
 * @var AsyncMongoDB $mongo
 */
use Echore\AsyncMongo\AsyncMongoDB;

$mongo->close();

InsertOne

use Echore\AsyncMongo\AsyncMongoDB;
use Echore\AsyncMongo\result\MongoInsertOneResult;

/**
 * @var AsyncMongoDB $mongo
 */

$mongo->insertOne(
    "databaseName",
    "collectionName",
    $document,
    $options
)->schedule(
    function(MongoInsertOneResult $result): void{
        echo "Inserted {$result->getInsertedCount()}!" . PHP_EOL;
        var_dump($result);
    }
    function(Throwable $e): void{
        echo "Error occurred: {$e->getMessage()}" . PHP_EOL;
    }
);

InsertMany

use Echore\AsyncMongo\AsyncMongoDB;
use Echore\AsyncMongo\result\MongoInsertManyResult;use Echore\AsyncMongo\result\MongoInsertOneResult;

/**
 * @var AsyncMongoDB $mongo
 */

$mongo->insertMany(
    "databaseName",
    "collectionName",
    $documents,
    $options
)->schedule(
    function(MongoInsertManyResult $result): void{
        echo "Inserted {$result->getInsertedCount()}!" . PHP_EOL;
        var_dump($result);
    }
    function(Throwable $e): void{
        echo "Error occurred: {$e->getMessage()}" . PHP_EOL;
    }
);

Find

use Echore\AsyncMongo\AsyncMongoDB;
use Echore\AsyncMongo\result\MongoCursorResult;use Echore\AsyncMongo\result\MongoInsertOneResult;

/**
 * @var AsyncMongoDB $mongo
 */

$mongo->find(
    "databaseName",
    "collectionName",
    $filter,
    $options
)->schedule(
    function(MongoCursorResult $result): void{
        $count = count($result->getArray());
        echo "Matched documents: {$count}!" . PHP_EOL;
        var_dump($result);
    }
    function(Throwable $e): void{
        echo "Error occurred: {$e->getMessage()}" . PHP_EOL;
    }
);

Set maximum number of results (5)

use Echore\AsyncMongo\AsyncMongoDB;
use Echore\AsyncMongo\result\MongoCursorResult;use Echore\AsyncMongo\result\MongoInsertOneResult;

/**
 * @var AsyncMongoDB $mongo
 */

$mongo->find(
    "databaseName",
    "collectionName",
    $filter,
    $options
)->limit(5)->schedule(
    function(MongoCursorResult $result): void{
        $count = count($result->getArray());
        echo "Matched documents: {$count}!" . PHP_EOL;
        var_dump($result);
    }
    function(Throwable $e): void{
        echo "Error occurred: {$e->getMessage()}" . PHP_EOL;
    }
);

FindOne

use Echore\AsyncMongo\AsyncMongoDB;
use Echore\AsyncMongo\result\MongoCursorResult;use Echore\AsyncMongo\result\MongoDocumentResult;use Echore\AsyncMongo\result\MongoInsertOneResult;

/**
 * @var AsyncMongoDB $mongo
 */

$mongo->findOne(
    "databaseName",
    "collectionName",
    $filter,
    $options
)->schedule(
    function(MongoDocumentResult $result): void{
        $count = $result->getDocument() !== null ? 1 : 0;
        echo "Matched document: {$count}!" . PHP_EOL;
        var_dump($result);
    }
    function(Throwable $e): void{
        echo "Error occurred: {$e->getMessage()}" . PHP_EOL;
    }
);

CountDocuments

use Echore\AsyncMongo\AsyncMongoDB;
use Echore\AsyncMongo\result\MongoCursorResult;use Echore\AsyncMongo\result\MongoDocumentResult;use Echore\AsyncMongo\result\MongoInsertOneResult;use Echore\AsyncMongo\result\MongoValueResult;

/**
 * @var AsyncMongoDB $mongo
 */

$mongo->countDocuments(
    "databaseName",
    "collectionName",
    $filter,
    $options
)->schedule(
    function(MongoValueResult $result): void{
        echo "Matched document: {$result->getPositiveInt()}!" . PHP_EOL;
    }
    function(Throwable $e): void{
        echo "Error occurred: {$e->getMessage()}" . PHP_EOL;
    }
);

Advanced Usage

Transaction

User buzz to User foo for 10 currency

use Echore\AsyncMongo\AsyncMongoDB;use Echore\AsyncMongo\session\SessionMediator;

/**
 * @var AsyncMongoDB $mongo
 */
 
$collection = $mongo->collection(
    "databaseName",
    "collectionName"
);

$mongo->transaction(
    function(array $successResults, array $errorResults, SessionMediator $session): void{
        if (count($errorResults) === 0){
            echo "No error occurred, committing" . PHP_EOL;
            $session->commit();
        } else {
            echo "Error occurred while transaction, aborting" . PHP_EOL;
            $session->abort();
        }
    },
    function(SessionMediator $session): void{
        echo "Transaction Completed" . PHP_EOL;
    },
    $collection->updateOne(
        ["user" => "foo"],
        [
            '$inc' => [
                "currency" => 10
            ]
        ]
    ),
    $collection->updateOne(
        ["user" => "buzz"],
        [
            '$inc' => [
                "currency" => -10
            ]
        ]
    )
);

asyncmongo's People

Contributors

skylake-git avatar

Stargazers

Shhelly avatar  avatar

Forkers

alis-dev-idn

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.