php Mongo DB MongoConnectionException' with message 'the connection has been terminated, and this cursor is dead

I am trying to get all records from a collection which has 5000 recs and loop through each record and do some operation and move it to another collection.

However after processing about 1100 recs , I get the following error

'MongoConnectionException' with message 'the connection has been terminated, and this cursor is dead'

How can I resolve ?

Answers


Mongo cursor timeout could be the reason. Set MongoCursor::$timeout=-1 for keeping cursor alive


It looks like this:

$mongoConn = new \MongoClient('mongodb://ip:port', ['socketTimeoutMS' => 120000]);
$mongoConn->connect();
$collection = $mongoConn->selectCollection('database_name', 'collection_name');
$cursor = $collection->find();
$cursor->timeout(120000);
while ($cursor->hasNext()) {
    $record = $cursor->getNext();
    //...
}
$mongoConn->close(); 

But you still can get an error "Maximum execution time of 30 seconds exceeded". This can be solved too:

ini_set("max_execution_time", '60');

or

set_time_limit (60);

or write in .htaccess:

php_value max_execution_time 60

But it's bad decisions. Sooner or later, the script does not run out of time to complete and you will get the same error. I would recommend you use indexes: http://docs.mongodb.org/manual/core/indexes/


Need Your Help

Could not open input file [COMPOSER]

php symfony composer-php sonata-user-bundle

when i'm running this command : php composer.phar require sonata-project/user-bundle --no-update

Google Map Direction Api using Retrofit

android gson google-maps-android-api-2 retrofit

I want to draw the route between two Location. I used the retrofit library to call the API. I didn't get any response. I need the polyline in an ArrayList. How i do that? Need help to create the