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

前端 未结 6 733
醉话见心
醉话见心 2020-12-16 20:32

I have updated mongo and now in the log the following error appears: Use of the aggregate command without the cursor option is deprecated

Mo

6条回答
  •  温柔的废话
    2020-12-16 21:12

    You have to use aggregateCursor which returns the cursor row instead of results only.

    Something like

    The first batch is by default set at 101 results.

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

    Set the batchsize ( the second parameter from your question ) on aggregate cursor of 50 for subsequent batches. If you don't use below option the default will fetch around 4 MB.

    $cur->batchSize( 50 );
    

    You can now iterate and read results to get all documents.

    The server will fetch initial(first) batch of 101 documents on first loop iteration followed by subsequent batch at 102 iteration and at intervals of 50 on rest of batches until you exhaust the cursor.

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

    To control batch size for first batch, you can specify batchSize as cursor option but generally it is not needed.

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

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

提交回复
热议问题