0
我尝试在PHP中使用this solution为分页:PHP MongoDB中找到查询
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
。我也试过这个查询,没有skip
和limit
,但结果仍然是一个空数组。如果$query
为空,则一切正常,它将返回聚集中的所有记录。
我在做什么错了?
SOLUTION$query = ["_id" => ['$gt' => new MongoId($currentId)]];
的实例也许'$ currentId'是错误的类型?看看你正在尝试做什么我认为它应该是'MongoId'的实例 – malarzm
你是对的。 '$ query = [“_id”=> ['$ gt'=> new MongoId($ currentId)]];'工作。请答复,以便我可以接受。 – ziwert
很好用,作为回答添加:) – malarzm