2017-02-08 4 views
1

PHPを使用してMongoDBに接続するのにMongoDB\Driver\Managerを使用しています。ドライバのバージョンは1.6.14で、私は接続してクエリを作成できます。PHP用MongoDBコネクター:ページ数の文書を数えてください

しかし、私は改ページを作るために私のクエリのためのドキュメントの合計数を必要とする:

$reg_pag = 20; 
$pag = $_GET["pag"]; 

$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 
$filter = []; 
$filter["brand"] = $_GET["brand"]; 
$options = ["skip" => ($pag-1)*$reg_pag , "limit" => $reg_pag]; 
$query = new MongoDB\Driver\Query($filter, $options); 
$rows = $mng->executeQuery("carsdb.cars", $query); 

私は$rows->count()とし、count($rows)てみてください。最初のコマンドは機能せず、最後のコマンドはフィルタされたデータを返します(return 20)。

答えて

0

executeCommandメソッドを使用してください。次のコードのようなものを試してみてください:

$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"; 
} 

は、あなたのケースに適応して、 hereから撮影。

+0

ありがとうございます!できます! :) – Josebyte

関連する問題