2017-02-08 5 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