2

I'm having troubles with PHP MongoCursor since I upgraded to Mongo PHP Driver from 1.5.8 to 1.6.0

The following code works well with version 1.5.8, but crashes with version 1.6

PHP version is 5.5.21., Apache version is Apache/2.4.10 (Ubuntu)

$mongoClient = new \MongoClient($serverUrl, ['readPreference'=>\MongoClient::RP_NEAREST]);
$database = $mongoClient->selectDB($dbName);
$collection = $database->selectCollection($collectionName);

// count() works fine and returns the right nb on documents
echo '<br/>count returned '.$collection->count();

// find() exectues with no error...
$cursor = $collection->find();
$documents = [];
// ...and hasNext() crashes with the Excetion below
while($cursor->hasNext()){$documents[] = $cursor->getNext();}
return $documents;

And so the hasNext() call crashes with this message :

CRITICAL: MongoException: The MongoCursor object has not been correctly initialized by its constructor (uncaught exception)...

Am I doing something wrong ? Thanks for you help !

1
  • @LeonardoDelfino: Please see my response below. I'd be interested to know if this is easily reproducible for you as well, and if you'd be willing to debug it further. Commented Feb 3, 2015 at 16:42

3 Answers 3

3

This may be related to a bug that was introduced in 1.6.0 regarding iteration with hasNext() and getNext(): PHP-1382. A fix has since been merged to the v1.6 branch and should be released later this week as 1.6.1.

That said, the bug regarding hasNext() was actually that the last document in the result set would be missed while iterating. If I run your original script against 1.6.0, the array contains a null value as its last element. With the fix in place, the array will contain all documents as is expected. I cannot reproduce the exception you're seeing with either version.

That exception is actually thrown from an internal checks on the C data structures, to ensure that the cursor object is properly associated with a MongoClient and socket connection. See the MONGO_CHECK_INITIALIZED() macro calls in this file. Most all of the cursor methods check that a MongoClient is associated, but hasNext() is unique in that it also checks for the socket object (I believe other methods just assume a cursor with a MongoClient also has a socket). If that exception is truly reproducible for you and you're willing to do some debugging with the extension, I'd be very interested to know which of the two checks is throwing the error.


As a side note, you should also be specifying the "replicaSet" option when constructing MongoClient. This should have the replica set name, which ensures that the driver can properly ignore connections to hosts that are not a member of the intended replica set.

Sign up to request clarification or add additional context in comments.

2 Comments

FYI: 1.6.1 missed an edge case with hasNext(). This should be fixed in 1.6.2, which was released today: github.com/mongodb/mongo-php-driver/releases/tag/1.6.2
1

I just encountered the same issue; I refactored my code to use the cursor iterator instead, ie:

foreach( $cursor as $doc ) {
 $documents[] = $doc;
}

Comments

1

I was looking for a code example of how to implement a tailable cursor and found this question. The following code is a simple example of a tailable cursor (via the $cursor variable) which you provide on a capped mongodb collection.

    $cursor->tailable(true);
    $cursor->awaitData(true);
    while (true) { 
        if ($cursor->hasNext()) { 
            var_dump($cursor->getNext()); 
        } else { 
            if ($cursor->dead()) { 
               break; 
            }
         } 
    }

2 Comments

Please add some details. Code-only answers aren't that useful.
Edited my comment to clarify. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.