我尝试在PHP中使用此解决方案进行分页:
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
的实例