【问题标题】:PHP MongoDB find queryPHP MongoDB 查找查询
【发布时间】:2016-08-13 12:21:42
【问题描述】:

我尝试在 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)]];

【问题讨论】:

  • 也许$currentId 的类型不对?看看你想要做什么,我认为它应该是 MongoId 的实例
  • 你说得对。 $query = ["_id" => ['$gt' => new MongoId($currentId)]]; 有效。请回答,以便我接受。
  • 很高兴它可以工作,作为答案添加:)

标签: php mongodb find


【解决方案1】:

也许$currentId 的类型不对?看看你想要做什么,我认为它应该是 MongoId 的实例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-24
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多