聚合函数的 MongoCursorTimeoutException [英] MongoCursorTimeoutException for aggregate function
问题描述
我正在尝试使用聚合函数从 Mongo 集合中获取一些数据,但它给了我 MongoCursorTimeoutException.我试图一次只选择 50 条记录,并且该集合有超过 1700 万条记录.这是从 PHP 完成的,我的代码如下:
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)
));
而 $filter_query 是另一个包含时间映射的数组,就像
and the $filter_query is another array which contains the timestmap and it is like
Array
(
[timestamp] => Array
(
[$gt] => 1383890400
[$lt] => 1384495200
)
)
我不知道为什么会发生这种情况,因为我试图只获取 50 条记录.我认为攻击是在选择所有记录后执行的,它导致了错误.除了侵略,还有什么更好的方法可以做到这一点?
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 ?
我可以将超时设置为 -1,但这是否是一个不错的选择,因为驱动程序将永远等待以获得初始响应?
I can set the timeout to -1, but is it a good option as the driver will wait forever to get the initial response ?
推荐答案
要为聚合函数设置超时选项,您应该使用 MongoDB 对象实例的 command
函数.例如:
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 )
);
更多信息请参考文档
这篇关于聚合函数的 MongoCursorTimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!