ziwert ziwert - 7 months ago 19
PHP Question

PHP MongoDB find query

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)]];

Answer

Maybe $currentId is of wrong type? Looking at what you're trying to do I think it should be instance of MongoId