How to set timeout to infinite to avoid MongoCursorTimeoutException in php

Mark Gill picture Mark Gill · Apr 6, 2011 · Viewed 22.8k times · Source

I am running the php script which fetch data from mongodb. I have a very huge database and I am getting this exception ..my mongodb version is 1.6.5

PHP Fatal error:  Uncaught exception 'MongoCursorTimeoutException' 
with message 'cursor timed out 
(timeout: 30000, time left: 0:0, status: 0)

My query is like this

private function executeRegex($start_date, $end_date, $source, $collection, $db)
  {
    $criteria = array(
        'date' => array(
          '$gte' => new MongoDate(strtotime($start_date)),
          '$lt' => new MongoDate(strtotime($end_date))
          ),  
        'uid'=> array(
          '$ne' => '-',
          ),  
        'source' => new MongoRegex($source)
        );  
    $value = $db->command(array('distinct' => $collection, 'key' => 'uid', 'query' => $criteria)); 
    return count($value['values']);
  }

how can I set timeout to infinite so that i do nt get this exception

Answer

zohreh picture zohreh · Oct 1, 2013

MongoCursor::$timeout = -1;

type above line in your php code. this set timeout for all queries . best regard,