3zzy 3zzy - 3 months ago 31
PHP Question

MongoCursorException - Cursor not found (MongoDB PHP Driver)

Code:

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


Error:


Fatal error: Uncaught MongoDB\Driver\Exception\RuntimeException:
Cursor not found, cursor id: 31837896248 in ...


It seems that the cursor does exist but times out? How can I prevent that from happening?

Edited to add: I tried doing:

if ($documentsFind) {
$documentsFind->immortal(true); // keep alive
foreach ($documentsFind as $product) {
// code...
}
}


But that results in
Call to undefined method MongoDB\Driver\Cursor::immortal()
.

Answer

Try querying like this:

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

find() method passes the 2nd argument to the Find class constructor, so you can see all the available options here

Comments