0

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

3
  • Maybe $currentId is of wrong type? Looking at what you're trying to do I think it should be instance of MongoId Commented Apr 20, 2016 at 17:54
  • You right. $query = ["_id" => ['$gt' => new MongoId($currentId)]]; works. Please, make an answer so I could accept it. Commented Apr 20, 2016 at 18:06
  • Glad it works, added as an answer :) Commented Apr 20, 2016 at 19:50

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.