Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP+MongoDB: Uncaught exception 'MongoCursorException' with message 'No such file or directory'

I'm working on a web application which is trying to connect to a MongoDB database from PHP. In the 90% of page loads everything works fine, but in the other 10% it throws the following exception when I try to update a collection:

Fatal error: Uncaught exception 'MongoCursorException' with message 'No such file or directory' in D:\webDev\webSites\str\dev3\_global_classes\User.php:40 Stack trace: #0 D:\webDev\webSites\str\dev3\_global_classes\User.php(40):    MongoCollection->update(Array, Array, Array) #1 D:\webDev\webSites\str\dev3\_init\_init.php(8):    User->__construct(NULL) #2 D:\webDev\webSites\str\dev3\index.php(3):    include('D:\webDev\webSi...') #3 {main} thrown in D:\webDev\webSites\str\dev3\_global_classes\User.php on line 40 

PHP code:

public function __construct($SESSIONID = null) {             User::$_users_collection = Main::$_mongo->selectCollection("users");       ...         $query = array('session_id' => session_id());      $expiry = time() + Main::$_lifetime;     $data = array(         'session_id' => session_id(),         'expiry' => (string)$expiry,         'ip' => $_SERVER['REMOTE_ADDR']     );      $options = array(         'upsert' => true,         'safe' => true     );      try {         User::$_users_collection->update($query, array('$set' => $data), $options);     } catch (Exception $e) {         throw $e;     }      ... } 

Mongo version:

Wed Oct 17 10:53:48 /usr/bin/mongos db version v2.0.7, pdfile version 4.5 starting (--help for usage) Wed Oct 17 10:53:48 git version: 875033920e8869d284f32119413543fa475227bf Wed Oct 17 10:53:48 build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41 

My mongo cluster has only one shard, my php version is: 5.4.4, and my mongo driver version is: 1.2.12.

like image 211
Norbert Avatar asked Nov 07 '12 17:11

Norbert


1 Answers

Check the error code $e->getCode(); using:

    try {         User::$_users_collection->update($query, array('$set' => $data), $options);     } catch (MongoCursorException $e) {     echo "error message: ".$e->getMessage()."\n";     echo "error code: ".$e->getCode()."\n";     } 

And than with the error code check the list of Mongo Cursor exception errors: http://www.php.net/manual/pt_BR/class.mongocursorexception.php

For example if you got error code 3 "This may indicate you are out of RAM or some other extraordinary circumstance.".

note: avoid the use of safe option, is deprecated. Use the WriteConcern w option, will provite more option too.

About the error: "Caused by accessing a cursor incorrectly or a error receiving a reply. Any operation that sends information to the database and waits for a response can throw a MongoCursorException. The only exception is new MongoClient() (creating a new connection), which will only throw MongoConnectionExceptions. " (http://php.net/manual/en/class.mongocursorexception.php)

like image 162
Diogo Baracho Avatar answered Oct 03 '22 19:10

Diogo Baracho