如何将超时设置为无限以避免 php 中的 MongoCursorTimeoutException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5561925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set timeout to infinite to avoid MongoCursorTimeoutException in php
提问by Mark Gill
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
我正在运行从 mongodb 获取数据的 php 脚本。我有一个非常大的数据库,我收到了这个异常..我的 mongodb 版本是 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
如何将超时设置为无限,以便我不会收到此异常
回答by zohreh
MongoCursor::$timeout = -1;
MongoCursor::$timeout = -1;
type above line in your php code. this set timeout for all queries . best regard,
在您的 php 代码中键入以上行。这为所有查询设置超时。最良好的问候,
回答by Gates VP
Here's documentation for setting the timeout on a cursor.
这是用于在 cursor 上设置超时的文档。
$cursor = $collection->find()->timeout(n);
The command
method does not return a cursor it returns an array.
该command
方法不返回游标,它返回一个数组。
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:
如果您查看command method的文档,您会发现它实际上只是以下内容的包装器:
public function command($data) {
return $this->selectCollection('$cmd')->findOne($data);
}
That should get you going in the right direction.
这应该让你朝着正确的方向前进。
回答by Krynble
The PHP driver supports a second argument in the command method to set the timeout.
PHP 驱动程序支持命令方法中的第二个参数来设置超时。
This means you should do the following:
这意味着您应该执行以下操作:
$value = $db->command(
array('distinct' => $collection, 'key' => 'uid', 'query' => $criteria),
array('timeout' => 10000000000)
);
This should solve your problem!
这应该可以解决您的问题!
回答by Victor Perov
I think, by the first step you should check how mongodb works with your query
我认为,第一步您应该检查 mongodb 如何处理您的查询
db.collection.find(...).explain()
And, if needs add indexes to specified fields by
并且,如果需要通过以下方式向指定字段添加索引
db.collection.ensureIndex(...)
And only than if your query is optimal, set timeout: -1
并且只有当您的查询是最佳的时,才设置超时:-1