简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM