代码:

try {
  $documentsFind = $client->$db->$collection->find([
      // query
  ]);
  if ($documentsFind) {
    foreach ($documentsFind as $product) {
    // code...
    }
  }
catch (MongoCursorException $e) {
  echo "error message: ".$e->getMessage()."\n";
  echo "error code: ".$e->getCode()."\n";
}

错误:
致命错误:uncaught mongodb\driver\exception\runtimeexception:
找不到游标,游标ID:31837896248 in…
似乎光标确实存在但超时了?我怎样才能防止这种情况发生?
编辑后添加:我尝试过:
 if ($documentsFind) {
    $documentsFind->immortal(true); // keep alive
    foreach ($documentsFind as $product) {
    // code...
    }
  }

但这会导致Call to undefined method MongoDB\Driver\Cursor::immortal()

最佳答案

尝试如下查询:

$documentsFind = $client->$db->$collection->find([
  // query
], ['noCursorTimeout' => true]);

find()方法将第二个参数传递给Find类构造函数,这样您就可以看到所有可用的选项here

关于php - MongoCursorException - 找不到游标(MongoDB PHP驱动程序),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37870115/

10-10 20:27