简体   繁体   English

PHP MongoDB查找查询

[英]PHP MongoDB find query

I try use this solution for pagination in PHP: 我尝试使用此解决方案在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 . 但是它返回一个空数组result Also I tried this query without skip and limit , but the result still was an empty array. 我也尝试了此查询而不使用skiplimit ,但是结果仍然是一个空数组。 If $query is empty, everything is ok, it returns all records in colection. 如果$query为空,则一切正常,它将返回所有选中的记录。

What I'm doing wrong? 我做错了什么?

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

Maybe $currentId is of wrong type? 也许$currentId类型错误? Looking at what you're trying to do I think it should be instance of MongoId 看看您要做什么,我认为它应该是MongoId实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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