I try use this solution for pagination in 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;
}
But it returns an empty array result. Also I tried this query without skip and limit, but the result still was an empty array. If $query is empty, everything is ok, it returns all records in colection.
What I'm doing wrong?
SOLUTION$query = ["_id" => ['$gt' => new MongoId($currentId)]];
$currentIdis of wrong type? Looking at what you're trying to do I think it should be instance ofMongoId$query = ["_id" => ['$gt' => new MongoId($currentId)]];works. Please, make an answer so I could accept it.