PHP MongoDB - Using an aggregate command without a cursor option is deprecated. What kind?

I updated Mongo, and now the following error appears in the log: Using the aggregate command without the cursor option is deprecated

Mongo says that I have to put the second REQUIRED parameter in the aggregate function because my current usage is deprecated.

I am currently using the following PHP code (not recommended now):

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

And return this format:

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

In order not to use outdated code, I add a new second parameter (but I don’t understand what to put):

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

And this returns the same information, but changes the original structure:

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

After updating Mongo forces me to change the way I read data. I don’t understand what value I should put in this second parameter called β€œcursor” ...

What should I add to this second parameter? Can I set a default value without changing the structure of the results?

Document: https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/ http://php.net/manual/es/mongocollection.aggregate.php

UPDATE:

If I point the cursor to a function, I no longer get the error. But, without turning to the solution, I read the log, and the warning appears randomly, I have code that I run several times, and sometimes, if it reports the warning, and others do not.

What for?

+9
function php mongodb php-mongodb
source share
6 answers

When you request something in MongoDB and expect results, you will have this variable called cursor , which is simply a pointer to the document you are currently reading. It’s like a scroll bar in a browser.

You can specify how many documents it should read into the batchSize buffer as you did with a value of 1 .

This is useful when you know how many documents you expect to read. When you need only 10 documents, you can get all these documents in one network packet using batchSize => 10 . If you specify batchSize => 5 , it will take more time, since it takes two network packets to the database to get the expected 10 documents.

You are safe using batchSize by default.

You can try to iterate over the cursor using foreach as in the example in the documentation: http://php.net/manual/en/class.mongocommandcursor.php

I am not sure if the php.net documentation matches the latest version of the MongoDB driver.

+5
source share

From the last MongoDB manual, an aggregate operation change has occurred.

aggregate without cursor

MongoDB 3.4 condemns the use of an aggregate command without a cursor, unless the pipeline includes an explanation parameter. when returning aggregation results using the aggregate command, specify the cursor parameter using the default batch size cursor: {} or specify the batch size in the cursor cursor: {batchSize:}.

You can simply specify this parameter to call the function with the addition of [ "cursor" => [ "batchSize" => 0 ] ] , since the second parameter will allow this. here .

You can also refer to this SO question for using cursor options.

+2
source share

You should use aggregateCursor , which returns a cursor row instead of results .

Something like

The first batch is set to 101 results by default.

 $cur = $this->db->{$collection}->aggregateCursor($pipeline); 

Set the batch size (second parameter from your question) on an aggregated rate of 50 for subsequent batches. If you do not use the option below, about 4 MB will be selected by default.

 $cur->batchSize( 50 ); 

Now you can iterate and read the results to get all the documents.

The server will receive the initial (first) batch of 101 documents in the first iteration of the cycle, and then the next batch in step 102 of the iteration and with an interval of 50 in the remaining lots until you run out of cursor.

 foreach ( $cur as $result ) { echo $result['_id'], "\n"; } 

To control the batch size for the first batch, you can specify batchSize as the cursor parameter, but usually it is not needed.

 $cur = $this->db->{$collection}->aggregateCursor($pipeline, 'cursor' => [ 'batchSize' => 1 ]); 

Link: https://derickrethans.nl/aggregation-cursor.html

+2
source share

Assuming you are using the latest MongoDB PHP Library , you should pass the option 'useCursor' => false (the default is true ), as described in the doc .

+1
source share

The mongo driver mongo outdated and does not support the latest major releases of PHP (e.g. PHP 7).

New driver named mongodb http://php.net/manual/en/set.mongodb.php

+1
source share

Replace this:
$ this-> db β†’ {$ collection} β†’ aggregate ($ options);

with the code below by adding an array of cursors.
$ this-> db β†’ {$ collection} β†’ aggregate ($ options, array ('cursor' => array ('batchSize' => 1)));

0
source share

All Articles