phpmongodbfunctionphp-mongodb

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


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' => 101)));

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

UPDATE:

If I indicate the cursor in the function I no longer receive the error. But without applying to the solution, I read the LOG and the warning appears at random, I have a code that I run several times and sometimes if it reports the mentioned warning and others do not.

Why?


Solution

  • When you query something to MongoDB and you expect results, you will have this variable called cursor, which simply is a pointer to the document you currently did read. It is just like a scrollbar in the browser.

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

    It is useful when you know how much documents you expect to read. When you only need 10 documents, you can get all those in a single network packet using batchSize => 10. When specify batchSize => 5, it will take longer because it does take two network packets to the database to get the expected 10 documents.

    You are safe using the default batchSize.

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

    Im not sure if the php.net documentation is up to date with the most current version of the MongoDB driver.