Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout to infinite to avoid MongoCursorTimeoutException in php

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

like image 527
Mark Gill Avatar asked Apr 06 '11 05:04

Mark Gill


2 Answers

MongoCursor::$timeout = -1;

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

like image 157
zohreh Avatar answered Oct 17 '22 00:10

zohreh


Here's documentation for setting the timeout on a cursor.

$cursor = $collection->find()->timeout(n);

The command method does not return a cursor it returns an array.

If you take a look at the documentation for the command method, you'll see that it's actually just a wrapper for the following:

public function command($data) {
    return $this->selectCollection('$cmd')->findOne($data);
}

That should get you going in the right direction.

like image 38
Gates VP Avatar answered Oct 16 '22 23:10

Gates VP