MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Connettore MongoDB per PHP:conta i documenti per l'impaginazione

Usa executeCommand metodo. Prova qualcosa come il seguente codice:

$mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// search params
$query = ["brand" => $_GET["brand"]];
// define a command - not only a regular query
$command = new MongoDB\Driver\Command(["count" => "cars", "query" => $query]);
try {
    // execute the command here on your database
    $result = $mongo->executeCommand("carsdb", $command);
    $res = current($result->toArray());
    $count = $res->n;
    echo $count;
} catch (MongoDB\Driver\Exception\Exception $e) {
    echo $e->getMessage(), "\n";
}

Tratto da qui , con un adattamento al tuo caso.