在我的PHP脚本中,find()查询仍然忽略了尝试mongodb全局超时等操作。我想要一个findOne({...})或find({...})查找,并在超时之前等待数据库服务器最多20ms。
如何确保PHP不会将此设置用作软限制?它仍然被忽略,甚至在5秒后处理答案。
这是一个PHP mongo驱动程序错误吗?
示例:
MongoCursor::$timeout=20;
$nosql_server=new Mongo('mongodb://user:pw@'.implode(",",$arr_replicas).'',array("replicaSet" => "gmt","timeout"=>10)) OR troubles("too slow to connect");
$nosql_db=$nosql_server->selectDB('aDB');
$nosql_collection_mcol=$nosql_db->mcol;
$testFind=$nosql_collection_mcol->find(array('crit'=>123));
//If PHP considered the MongoCursor::$timeout, I'd expect the prev. line to be skipped or throwing a mongo/timeout exception if DB does not return the find result cursor ready within 20ms.
//However, I arrive with this line after seconds, without exception whenever the DB has some lock or delay, without skipping previous line.发布于 2013-11-05 21:05:07
在$timeout的PHP文档中,以下是对游标超时的解释:
如果查询花费的时间超过指定的毫秒数,则
会导致获取结果的方法抛出MongoCursorTimeoutException。
我认为超时是指在游标上执行的操作(例如getNext())。
发布于 2014-06-04 03:32:47
不要这样做:
MongoCursor::$timeout=20;这是在调用静态方法,对AFAIK没有任何好处。
您需要认识到的是,在您的代码示例中,$testFind是MongoCursor对象。因此,在您给出的代码片段中,您应该在其他内容之后添加以下内容,以便设置$testFind MongoCursor的超时时间:
$testFind->timeout(100);注意:如果您希望将$testFind作为一个数组来处理,则需要执行以下操作:
$testFindArray = iterator_to_array($testFind);那一次让我大吃一惊。希望这对某些人有帮助。
发布于 2015-09-26 04:07:19
注意readPreference属性。可能的值包括:
MongoClient::RP_PRIMARY
MongoClient::RP_PRIMARY_PREFERRED
MongoClient::RP_SECONDARY
MongoClient::RP_SECONDARY_PREFERRED
MongoClient::RP_NEARESThttps://stackoverflow.com/questions/19759201
复制相似问题