Yara_M
Yara_M

Reputation: 633

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

Upvotes: 0

Views: 56

Answers (1)

malarzm
malarzm

Reputation: 2966

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

Upvotes: 1

Related Questions