MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

Enregistrements de comptage PHP MongoDB

J'ai trouvé une bien meilleure solution Commande MongoDB

Le voici

<?php
    // Last Online Time
    $Time = time() - 86400;

    // Connection
    $Manager = new MongoDB\Driver\Manager("mongodb://" . DB_USERNAME . ":" . DB_PASSWORD . "@" . DB_HOST . ":" . DB_PORT . "/" . DB_NAME);

    // Command
    $Command = new MongoDB\Driver\Command(["count" => "account", "query" => ['LastOnline' => ['$gt' => (int) $Time]]]);

    // Result
    $Result = $Manager->executeCommand(DB_NAME, $Command);

    //print($Result->toArray());Array ( [0] => stdClass Object ( [n] => 228598 [ok] => 1 ) //so n is our totalcount
    // Get Total Online In 1 Hour Ago
    echo count($Result->toArray()[0]->n);
?>