Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoCursorTimeoutException in jenssegers/laravel-mongodb

I have a query which looks up data in a huge collection (over 48M), yet even if I add timeout=-1 to it, it still throws MongoCursorTimeoutException exception..

return \DB::connection('mongodb')->collection('stats')->timeout(-1)
    ->where('ip','=',$alias)
    ->where('created_at','>=', new \DateTime( $date ) )
    ->where('created_at','<=', new \DateTime( $date . ' 23:59:59' ) )
    ->count();

I am using this library: https://github.com/jenssegers/laravel-mongodb

Any ideas?

like image 360
Broshi Avatar asked Jul 02 '15 16:07

Broshi


1 Answers

There is an issue PHP-1249 - MongoCursor::count() should use cursor's socket timeout submitted for PHP MongoDB driver v1.5.7 which was fixed in v1.5.8 in October, 2014.

The reply from the support:

Looking into the code a bit, it appears that both socket timeout and maxTimeMS is not passed along to the count command.

If you need an immediate work-around, you should be able to get by with MongoDB::command() for now (which can support both timeouts).

The workaround posted by one of the users is:

$countComand = $mongo->command(
    array(
        'count' => 'collection',
        'query' => $query
    ),
    array('socketTimeoutMS' => -1)
);

 
if($countComand['ok']){
    $count = $countComand['n'];
} else {
    // ... error ...
}

It seems that laravel-mongodb doesn't use MongoDB::command(). You either have to write your query explicitly without help of where methods as shown above or upgrade to v.1.5.8.

like image 194
Gyrocode.com Avatar answered Oct 08 '22 22:10

Gyrocode.com