聚合函数的 MongoCursorTimeoutException

编程入门 行业动态 更新时间:2024-10-11 21:23:34
本文介绍了聚合函数的 MongoCursorTimeoutException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用聚合函数从 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

本文发布于:2023-11-22 01:34:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   MongoCursorTimeoutException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!