<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Using a Cursor to Get All of the Documents</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.counting.html">Counting Documents in A Collection</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.criteria.html">Setting Criteria for a Query</a></div> <div class="up"><a href="mongo.tutorial.html">Tutorial</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongo.tutorial.cursor" class="section"> <h2 class="title">Using a Cursor to Get All of the Documents</h2> <p class="para"> In order to get all the documents in the collection, we will use <span class="function"><a href="mongocollection.find.html" class="function">MongoCollection::find()</a></span>. The find() method returns a <a href="class.mongocursor.html" class="classname">MongoCursor</a> object which allows us to iterate over the set of documents that matched our query. So to query all of the documents and print them out: <div class="example" id="mongo.tutorial.cursor-example"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$connection </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$connection</span><span style="color: #007700">-></span><span style="color: #0000BB">database</span><span style="color: #007700">-></span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">find</span><span style="color: #007700">();<br />foreach ( </span><span style="color: #0000BB">$cursor </span><span style="color: #007700">as </span><span style="color: #0000BB">$id </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value </span><span style="color: #007700">)<br />{<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">: "</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">$value </span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> and that should print all 101 documents in the collection. <em>$id</em> is the <em>_id</em> field of a document (cast to a string) and <em>$value</em> is the document itself. </p> <div class="section" id="mongo.tutorial.cursor.seealso"> <h2 class="title">See Also</h2> <p class="para"> The API documentation on <span class="function"><a href="mongocollection.find.html" class="function">MongoCollection::find()</a></span> contains more information about finding data. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.counting.html">Counting Documents in A Collection</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.criteria.html">Setting Criteria for a Query</a></div> <div class="up"><a href="mongo.tutorial.html">Tutorial</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>