PHP MongoDB查找查询

时间:2016-04-20 16:51:10

标签: php mongodb find

我尝试在PHP中使用this solution分页:

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;
}

但它返回一个空数组result。此外,我在没有skiplimit的情况下尝试了此查询,但结果仍然是一个空数组。如果$query为空,一切正常,它会返回收集中的所有记录。

我做错了什么?


$query = ["_id" => ['$gt' => new MongoId($currentId)]];

1 个答案:

答案 0 :(得分:1)

也许$currentId类型错误?看看你想要做什么,我认为它应该是MongoId

的实例