PHP MongoDB - Use of the aggregate command without the cursor option is deprecated. What?

737 views
Skip to first unread message

Ephram

unread,
May 8, 2017, 3:51:28 PM5/8/17
to mongodb-user

I have updated mongo and now in the log the following error appears: 

Use of the aggregate command without the cursor option is deprecated


Mongo says that I should put a second REQUIRED parameter to aggregate function, because my current usage is deprecated.


I currently use the following code PHP (now is deprecated):

$this->db->{$collection}->aggregate($options);


And return this format:

{"result":[
    {
    "_id":"xxxxxx",
    "update":[
    {
    "firstUpdateTime":xxxxxx,
    "updateTime":xxxxxxx,
    }
    ],
    "media":[
    {
    "xxxx":{ ...


To not use an deprecated code I add the new second parameter (but I do not understand what to put):

$this->db->{$collection}->aggregate($options, array('cursor' => array('batchSize' => 1)));


And this returns the same information but varies the initial structure:

{"cursor":{
"id":{
"value":"xxxxxx"
},
"ns":"xxxxxx.instagram",
"firstBatch":[
{
"_id":"xxxxxx",
"update":[
{
"firstUpdateTime":xxxxxx,
"updateTime":xxxxxx,
}
],
"media":[
{
"xxxxxx":{ ...



After the update Mongo forces me to change the way I read the data. I don't understand what value I should put in that second parameter called "cursor"...


What should I put in that second parameter? Can I set a default value without altering the structure of results?


Doc: 

https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/

http://php.net/manual/es/mongocollection.aggregate.php

http://stackoverflow.com/questions/43819325/php-mongodb-use-of-the-aggregate-command-without-the-cursor-option-is-deprecat

Jeremy Mikola

unread,
May 8, 2017, 4:07:08 PM5/8/17
to mongod...@googlegroups.com
On Mon, May 8, 2017 at 8:55 AM, Ephram <eph...@gmail.com> wrote:

What should I put in that second parameter? Can I set a default value without altering the structure of results?


Per the aggregate command documentation, you can provide an empty document as the value of the "cursor" option to specify the default batch size. Since the PHP driver would convert an empty array to a BSON array and the command does check for a BSON document, you'll need to use an empty object (e.g. `(object) []`, `new stdClass`).

That said, the MongoCollection::aggregateCursor() method should be used for this purpose, as it will return an iterable MongoCommandCursor. MongoCollection::aggregate() returns a raw command result, which only contains the first batch of results and a cursor ID to request additional results. If you are working with a raw command result, you'll need to construct a cursor using the MongoCommandCursor::createFromDocument() factory method -- this is essentially what MongoCollection::aggregateCursor() does for you internally.

As you're using the legacy "mongo" extension, you should note that the newer "mongodb" extension and userland library have a MongoDB\Collection::aggregate() method that provides a much improved API for this.
Reply all
Reply to author
Forward
0 new messages