Klaus Klaus - 23 hours ago 2
PHP Question

MongoDB find cursor vs toArray iteration and num requests

I am using MongoDB on php.
When I do a find, a MongoDB\Driver\Cursor is returned.

What is the better approach to iterate over the retrieved data?

foreach($cursor as $entry){}


foreach($cursor->toArray() as $entry){}

How many requests to the server are send on each approach?
If more than one, is there a way to retrieve all data with one request?

Thanks in advance.


MongoDB returns documents in batches (the default batch size id 101), as soon as you start iterating the cursor. On first iteration a call hit to server to get the documents in the first batch and driver store these documents locally. On subsequent iterations driver serve documents from local until the iteration reaches to the batch size. So, on the 102th iteration driver again send call to the server to get the next batch and so on.

You can set the batch size by providing the optional parameter batchSize in find method, More help can be find here

approach 1:

foreach($cursor as $entry){}

approach 2:

foreach($cursor->toArray() as $entry){}

$cursor->toArray implemented in such a way that it Iterates the cursor and returns it's results in an array. So the same number of calls made to server when you use either of these approaches. The only difference is that you will get a single array of result set by using approach 2.

Drawback of using approach 2:

  • The result set iterated twice, once in toArray method and once when you get entry from array.
  • You might get memory issues (out of memory) if you have large number of documents in result set.

Answer to your question:

Approach 1 would be better to implement, if you want to minimize the number of calls to server during cursor iterations just set the batchSize explicitly (by providing optional parameter batchSize in find method).