php - MongoCursorException - 未找到游标(MongoDB PHP 驱动程序)

标签 php mongodb exception cursor timeout

代码:

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";
}

错误:

Fatal error: Uncaught MongoDB\Driver\Exception\RuntimeException: Cursor not found, cursor 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/

相关文章:

node.js - Openshift 3 应用程序部署失败 : Took longer than 600 seconds to become ready

mongodb - Docker-Compose 拒绝从 Express App 到 MongoDB 容器的连接

Java - 避免传递已检查的异常

php - MySQL SELECT 查询语法 - 没有按预期工作?

php - 在表单验证之前设置实体参数

java - 在 Spring 中使用查找进行聚合查询

java - 使用 JavaBrains 中的代码时出现异常

iphone - 如何在 iPhone 应用程序中进行应用程序级别的异常处理

PHP:MySQL 变量值在数组中没有改变?

javascript - PHP乘法和减法脚本