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

5

29

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.

Shit answered 7/11, 2012 at 17:24 Comment(3)
Is there anything relevant in MongoDB server log?Soracco
Mongo client PECL version is at 1.3.6. I would suggest trying a later version as this has been under active development for a while. github.com/mongodb/mongo-php-driver/tagsPamulapan
Can you try to echo the code? That should help us analyze the problem in detail. Please use $e->getCode() in the catch block.Omnifarious
A
2

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)

Aquileia answered 6/6, 2013 at 16:37 Comment(0)
M
0

hard to know where it happens, but this page explains in detail what can cause this exception

Marcille answered 6/12, 2012 at 8:59 Comment(0)
T
0

Probably, the PHP Mongo Client encounters a failure while connecting to the actual MongoDB.

As you stated somewhere else (https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/wqcCNQgiJAQ), you run MongoDB through VMWare, which might cause connection issues now and then.

Triform answered 14/5, 2013 at 9:5 Comment(0)
E
0

I've seen this error quite a bit and it makes sense:

MongoDB raises cursor exceptions when there is a TIMEOUT during cursor iteration. That is, if you issue a query on Mongo for a large dataset, allocating a cursor. Then, you iterate over the cursor, retrieving your results one at a time, which is normal cursor behavior. BUT, if you're still retrieving results after a long enough time, you get the above message.

The solution for this could be two things:

  • (app layer) Retrieve your results into a temporary collection and iterate over that (ugly);
  • (connection layer) Set your timeout higher before querying (much better presuming it's possible).

Note your results may vary, best of luck.

Ermin answered 4/6, 2013 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.