Migrate to new mongodb php Driver - Aggregation Framework doesn't work

143 views
Skip to first unread message

Bodenhaltung

unread,
Mar 24, 2017, 8:01:37 AM3/24/17
to mongodb-user
Hi,

i am working on the migration to the new php driver from mongodb, at this moment i have no idea what exactly the problem is, the query works fine with the old driver, actually i am getting no query or php error, here the print_r from the executed aggregation: https://gist.github.com/Bodenhaltung/b46e24b5b204eb3353bd5e731e877820

There is no "result" array, and as i said, i get not really a error message, or miss i something?

Thanks for your help and pointing me in the right way.


Best regards,

Norbert

Jeremy Mikola

unread,
Mar 24, 2017, 10:55:03 AM3/24/17
to mongod...@googlegroups.com
On Fri, Mar 24, 2017 at 8:01 AM, Bodenhaltung <bodenh...@gmail.com> wrote:
Hi,

i am working on the migration to the new php driver from mongodb, at this moment i have no idea what exactly the problem is, the query works fine with the old driver, actually i am getting no query or php error, here the print_r from the executed aggregation: https://gist.github.com/Bodenhaltung/b46e24b5b204eb3353bd5e731e877820

Since MongoDB 2.6, the aggregate command has supported a "cursor" option, which returns the aggregation's results in a cursor instead of a single command result document with a "results" array field. Since the legacy driver's MongoCollection::aggregate() command was documented as returning a PHP array, we could not change this without breaking backwards compatibility and were forced to introduce a MongoCollection::aggregateCursor() method that returned a cursor object.

In the new driver, MongoDB\Collection::aggregate() is simply documented as returning a Traversable object. Since we default the "useCursor" to true when executing the command on a 2.6+ server, you will typically get back a MongoDB\Driver\Cursor object. If you explicitly disable the option or issue the command on an older server, we will wrap the old "results" array in an ArrayIterator, which still allows us to give a Traversable object back to the user.

There is no "result" array, and as i said, i get not really a error message, or miss i something?

Just like with a normal query, you need to iterate on the cursor to obtain the aggregation results. If the result is small, you can also use MongoDB\Driver\Cursor::toArray() to iterate over all of the results (including subsequent batches) and collect them in a single PHP array. This is effectively the same as calling iterator_to_array() on a Cursor object. Note that this approach will use more memory than standard iteration with a foreach loop.
 

Thanks for your help and pointing me in the right way.


Best regards,

Norbert

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/26ce80ae-eaf6-4031-9c22-d216ab430145%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bodenhaltung

unread,
Mar 25, 2017, 10:08:17 AM3/25/17
to mongodb-user
Hi,


Am Freitag, 24. März 2017 15:55:03 UTC+1 schrieb Jeremy Mikola:

Just like with a normal query, you need to iterate on the cursor to obtain the aggregation results. If the result is small, you can also use MongoDB\Driver\Cursor::toArray() to iterate over all of the results (including subsequent batches) and collect them in a single PHP array. This is effectively the same as calling iterator_to_array() on a Cursor object. Note that this approach will use more memory than standard iteration with a foreach loop.

Perfect, thank you for your answer in detail, now i get it. :)


Best regards,

Norbert 
Reply all
Reply to author
Forward
0 new messages