2016-04-20 78 views
0

我尝试在PHP中使用this solution为分页:PHP MongoDB中找到查询

public function getRecords($page, $count, $currentId) 
{ 
    $query = ["_id" => ['$gt' => $currentId]]; //what's wrong here? 
    $cursor = $this->recordsCollection->find($query)-> 
    skip(($page-1) * $count)-> 
    limit($count)-> 
    sort(["_id" => true]); 

    $result = array(); 
    foreach ($cursor as $doc) { 
     array_push($result, $doc); 
    } 
    return $result; 
} 

但它返回一个空数组result。我也试过这个查询,没有skiplimit,但结果仍然是一个空数组。如果$query为空,则一切正常,它将返回聚集中的所有记录。

我在做什么错了?

SOLUTION
$query = ["_id" => ['$gt' => new MongoId($currentId)]];

+0

的实例也许'$ currentId'是错误的类型?看看你正在尝试做什么我认为它应该是'MongoId'的实例 – malarzm

+0

你是对的。 '$ query = [“_id”=> ['$ gt'=> new MongoId($ currentId)]];'工作。请答复,以便我可以接受。 – ziwert

+0

很好用,作为回答添加:) – malarzm

回答

1

也许$currentId是错误的类型?看看你想做什么我认为它应该是MongoId