语法问题限制使用find()MongoDB PHP返回的文档数

I'm using find() to fill a php table, and as the database grows I want to limit the results to say 3 documents from the collection. According to the documentation $cursor = $collection->find().limit(3) should accomplish this but no (gives me nothing). Can anyone enlighten me what the issue may be?

edit: error from putty is 'PHP Fatal error: Call to undefined function limit()'

 try{
   $cursor = $collection->find();
    $largest =  0;
   foreach($cursor as $document){
   if ($largest < $document["Time"]) {
       $largest = $document["Time"];
       $longitude = $document["Longitude"];
       $latitude = $document["Latitude"];
   }

   $data .= "<tr>";
   $data .= "<td>" . $document["Time"] . "</td>";
   $data .= "<td>" . $document["Longitude"]."</td>";
   $data .= "<td>" . $document["Latitude"]."</td>";
   $data .= "</tr>";
  }
  $data .= "</tbody>";
  $data .= "</table>";
  echo $data;
  }catch(MongoException $mongoException){
    print $mongoException;
    exit;
}

visual representation, only 2 documents in the collection atm: only 2 documents in the collection atm

dongsha1544
dongsha1544 很不幸的是,不行。我从来没有看到以这种方式使用限制。我认为.limit()是正确的想法。->limit(3)返回:'调用未定义的方法MongoDB\Driver\Cursor::limit()'
一年多之前 回复
dongtu0088
dongtu0088 这看起来不正确:$cursor=$collection->find()。limit(3),也许它是:$cursor=$collection->find()->limit(3)。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐