title: mongoDB cursor difference between foreach loop and while loop
body: <p>I am using mongoDB PHP, when I use find query or aggregation framework, the Result is a cursor, I can iterate over the cursor using foreach loop or while loop. For example I have the following query to execute</p>
  
  <pre><code>$result = $collection-&gt;find();
  </code></pre>
  
  <p>I am able to iterate over the result using following two methods</p>
  
  <pre><code>1- foreach($result as $mongoid =&gt; $doc) {
          echo "mongoid is " . $mongoid;
          print_r($doc);
      }
  
  2- while ( $result-&gt;hasNext() ) {
         $doc = $result-&gt;getNext();  
         $result-&gt;next();  
         print_r( $doc );
         echo "find one doc in cursor\n";
  
     }
  </code></pre>
  
  <p>Which of the above iteration method is best and why?</p>