doctrine/mongodb

Aggregate error

DesignJobs opened this issue ยท 9 comments

Hi got error
MongoDb version 3.6.1

CRITICAL - Uncaught PHP Exception Doctrine\MongoDB\Exception\ResultException: "The 'cursor' option is required, except for aggregate with the explain argument" at /var/www/html/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Collection.php

Seems like you should use cursor option...

Sorry but I can't say anything more with just the exception that tells you what is wrong.

$builder->execute(['cursor' => '']); worked!! Thanks

As of MongoDB 3.6, the server prohibits non-cursor aggregations:

Changed in version 3.6: MongoDB 3.6 removes the use of aggregate command without the cursor option unless the command includes the explain option. Unless you include the explain option, you must specify the cursor option.

source: https://docs.mongodb.com/manual/reference/command/aggregate/

@DesignJobs: Can you share a full code example (feel free to redact the pipeline if needed) of what you were running?

@malarzm: If this was using Doctrine\MongoDB\Aggregation\Builder, we may want to start defaulting cursor to an empty document. This would come at the expense of MongoDB 2.4, since cursor was introduced in 2.6; however, the current drivers have already dropped support for 2.4 (see: PHPC-995).

$builder->execute(['cursor' => '']);

For the record, the cursor option should be a BSON document with an optional batchSize integer field within (without out, the default batch size is used). I'm not sure why the server accepts an empty string in your case, but I would advise you use (object) [] or new stdClass instead. IIRC, previous server versions have certainly raised errors if cursor was not a BSON document (including an empty PHP array, which serializes to a BSON array).

I had exactly the same problem;
empty string|array or new stdClass both works well
it would be really helpful to have this by default

FWIW, I looked at providing a fix to the issue, but it could be seen as a BC break since suddenly we no longer return an array result, but rather an iterator with the result of the aggregation pipeline. Thus, if you're using MongoDB 3.6 you'll have to make the appropriate changes yourself.

If you are using Doctrine MongoDB ODM 1.2, it brings its own aggregation builder that always provides the cursor option and returns an iterator with the results. If you are using this library stand-alone without MongoDB ODM, please consider switching to mongodb/mongodb, as this library is in bug-fixing mode only and only gets a limited amount of development time.

Hi @alcaeus, we are moving to MongoDB 4.0.8 and we are using the following packages and also have this cursor error message.

  • alcaeus/mongo-php-adapter v1.1.6
  • doctrine/mongodb v1.6.3
  • doctrine/mongodb-odm v1.2.8
  • mongodb/mongodb 1.4.2

Since all the packages are up to date, what's your recommendation? Should we add cursor: "" to our aggregates?

As indicated in my comment above, the aggregation builder in ODM always provides the cursor option: https://github.com/doctrine/mongodb-odm/blob/1.2.8/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php#L73

If you are not using the ODM aggregation builder, you'll have to manually specify this option to avoid the error in question. This library is no longer maintained as it is replaced by the mongodb/mongodb library.

Should we add cursor: "" to our aggregates?

@olvlvl: If you're manually executing an aggregate command, the cursor option should be an empty document (unless you mean to specify a batch size). In PHP, this would be 'cursor' => (object) [] at a minimum. See the linked manual docs for more details.