简体   繁体   中英

MongoCursorTimeoutException for aggregate function

I am trying to fetch some data from the Mongo collection using aggregate function, but it is giving me MongoCursorTimeoutException. I am trying to select only fifty records at a time and the collection has over 17M records. This is done from PHP and my code is as follows :

$data = $my_collection->aggregate(array(
                                   array('$match'=>$filter_query),
                                   array('$group'=>array('_id'=>'$email')), 
                                   array('$skip'=>$offset),    
                                   array('$limit'=>$per_page)
                                 ));

and the $filter_query is another array which contains the timestmap and it is like

Array
(
    [timestamp] => Array
        (
            [$gt] => 1383890400
            [$lt] => 1384495200
        )

)

I am not sure why this is happening as I am trying to fetch only 50 records. I think the agrression is performed after selecting all the records and it is causing the error. Any better method to do this other than aggression ?

I can set the timeout to -1, but is it a good option as the driver will wait forever to get the initial response ?

To set timeout option for aggregate function you should use command function of MongoDB object instance. For example:

$result = $database->command(
array(
    'aggregate' => $my_collection,
    'pipeline' => array(
        array('$match' => $filter_query),
        array('$group' => array('_id'=>'$email')),
                    array('$skip'=>$offset),
        array('$limit'=>$per_page)
    )
),
array( 'timeout' => $timeout )
);

For more information please refer to documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM