14

The PHP documentation for the mongo class says using a cursor instead of iterator_to_array is superior.

Why? What benefits/flexibility will I get from that?

1
  • 1
    Fortunately, the docs have been edited since the time this question was asked, and they justify their advice clearly and explicitly now on the very page you've linked to. Commented May 13, 2015 at 23:31

1 Answer 1

21

Using iterator_to_array() makes your driver load all of the results into memory at once, and you could easily run out of memory. This would not be the case with a cursor, which uses lazy-loading!

Straight from the linked docs:

<?php

$cursor = $collection->find();
var_dump(iterator_to_array($cursor));

?>

...

Suppose that, in the example above, $collection was a 50GB collection. We certainly wouldn't want to load that into memory all at once, which is what a cursor is for: allowing the client to access the collection in dribs and drabs.

Sign up to request clarification or add additional context in comments.

1 Comment

Im' using $arrResults = $cursor->toArray(); and i got crash when the large records. Maybe this load all of the results into memory at once, too. How can return all values to client

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.