alcaeus / mongo-php-adapter

:link: Adapter to provide ext-mongo interface on top of mongo-php-library
MIT License
464 stars 125 forks source link

Mongo 3.6 with cursors : "cursors option is required, except for aggregate with explain" #226

Closed arendjantetteroo closed 6 years ago

arendjantetteroo commented 6 years ago

Since upgrading to Mongo 3.6 I receive the following error message. This does not happen on 3.4.14.

Uncaught PHP Exception Doctrine\MongoDB\Exception\ResultException: "The 'cursor' option is required, except for aggregate with the explain argument"

I traced it back to line 161 in MongoCollection.php -> aggregate function.

if (isset($options['cursor'])) {
        $options['useCursor'] = true;

        if (isset($options['cursor']['batchSize'])) {
            $options['batchSize'] = $options['cursor']['batchSize'];
        }

        unset($options['cursor']);
    } else {
        $options['useCursor'] = false;  (line 161)
    }

If i don't define a ['cursor' => true] in my options on aggregate() method calls, it defaults to false, but this seems to give the error above. If i either remove that line or change it to true, it works again (with one caveat that places that don't expect a cursor need changes in my code).

The same issue happens when calling execute() on an aggregationBuilder without passing ['cursor' => true].

I've looked at the mongo docs but i didn't find a place where this behaviour for cursors in aggregate was changed. (or maybe this happened in php mongo 1.4 in combination with mongo 3.6 which i also updated from 1.3.3)

Sorry if this needs to go somewhere else.

alcaeus commented 6 years ago

If i don't define a ['cursor' => true] in my options on aggregate() method calls, it defaults to false, but this seems to give the error above.

This is correct and somewhat expected: this library replicates the behavior of the legacy driver, which by default doesn't use cursors for aggregation pipelines. This is why it specifies useCursor => false when passing options to the MongoDB library. If you need your aggregations to run on MongoDB 3.6, you'll have to change the method calls in your code.

From your exception message, I can see that you're using the aggregation builder from doctrine/mongodb. If you are using that library standalone, you might want to migrate to mongodb/mongodb as we're no longer actively developing the library. If you are using doctrine/mongodb-odm, upgrading to 1.2 will give you a more powerful aggregation builder which automatically specifies the cursor option and supports hydration of result documents into objects. You can read more about aggregation builder in the ODM documentation.

arendjantetteroo commented 6 years ago

Thanks, we already use mongodb-odm 1.2.

Ok, that's unfortunate as we use these in lots of places. Is there an easy way to override the MongoCollection.php file, so i can change the default to true?