MongoDB - Why should I use a cursor instead of iterator_to_array (in PHP)

懵懂的女人 提交于 2019-12-04 00:31:19

问题


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:


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.



来源:https://stackoverflow.com/questions/5060956/mongodb-why-should-i-use-a-cursor-instead-of-iterator-to-array-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!