【发布时间】:2016-08-13 12:21:42
【问题描述】:
我尝试在 PHP 中使用this solution 进行分页:
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。我也尝试了没有skip 和limit 的查询,但结果仍然是一个空数组。如果$query 为空,则一切正常,返回集合中的所有记录。
我做错了什么?
解决方案$query = ["_id" => ['$gt' => new MongoId($currentId)]];
【问题讨论】:
-
也许
$currentId的类型不对?看看你想要做什么,我认为它应该是MongoId的实例 -
你说得对。
$query = ["_id" => ['$gt' => new MongoId($currentId)]];有效。请回答,以便我接受。 -
很高兴它可以工作,作为答案添加:)