【问题标题】:MongoCursorException - Cursor not found (MongoDB PHP Driver)MongoCursorException - 找不到光标(MongoDB PHP 驱动程序)
【发布时间】:2016-10-18 14:46:34
【问题描述】:

代码

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

错误

致命错误:未捕获的 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()

【问题讨论】:

  • 第一次运行时会出现错误还是您可以加载一些条目?
  • 它从数据库中获取了相当多的文档,但有时会超时。我可以用mongod --setParameter cursorTimeoutMillis=9000000 暂时绕过这个

标签: php mongodb exception cursor timeout


【解决方案1】:

尝试这样查询:

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

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

【讨论】:

    【解决方案2】:

    Cursor exception 说,

    驱动程序试图从数据库中获取更多结果,但数据库没有查询记录。这通常意味着游标在服务器端超时:几分钟不活动后,数据库将终止游标。

    MongoDB PHP 驱动程序有两种不同的超时:

    1. 连接超时
    2. 光标超时

    确保在光标上使用超时或不朽:

    $cursor = $collection->find();
    $cursor->immortal(true); 
    $cursor->timeout(-1);
    

    注意:Timeout 表示在客户端等待的时间,而immortal 在服务器端设置光标。

    但我建议如果你有大数据大小的游标,那么你应该以块的形式获取游标数据,例如:

    从集合中获取前 1000 个文档,处理它们。然后获取接下来的 1000 个文档。您可以通过跳过和限制来做到这一点。

    【讨论】:

    • 尝试了所有这些。 immortaltimeout 似乎不适用于新的 PHP 驱动程序 mongodb,而不是 mongo
    • 您使用的是哪个 mongodb 版本?您是否正在获取 find() 的光标对象?你有哪个 php mongo 驱动程序? @3zzy
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多