<!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>Gets an array of names for all collections in this database</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb.getcollectioninfo.html">MongoDB::getCollectionInfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb.getdbref.html">MongoDB::getDBRef</a></div> <div class="up"><a href="class.mongodb.html">MongoDB</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongodb.getcollectionnames" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoDB::getCollectionNames</h1> <p class="verinfo">(PECL mongo >=1.3.0)</p><p class="refpurpose"><span class="refname">MongoDB::getCollectionNames</span> — <span class="dc-title">Gets an array of names for all collections in this database</span></p> </div> <div class="refsect1 description" id="refsect1-mongodb.getcollectionnames-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>MongoDB::getCollectionNames</strong></span> ([ <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = array()</span></span> ] )</div> <p class="para rdfs-comment"> Gets a list of all collections in the database and returns their names as an array of strings. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This method will use the <a href="https://docs.mongodb.com/manual/reference/command/listCollections" class="link external">» listCollections</a> database command when communicating with MongoDB 2.8+. For previous database versions, the method will query the special <em>system.namespaces</em> collection.</span></p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-mongodb.getcollectionnames-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">options</code> </dt> <dd> <p class="para"> An array of options for listing the collections. Currently available options include: <ul class="itemizedlist"> <li class="listitem"><p class="para"><em>"filter"</em></p><p class="para">Optional query criteria. If provided, this criteria will be used to filter the collections included in the result.</p><p class="para">Relevant fields that may be queried include <em>"name"</em> (collection name as a string, without the database name prefix) and <em>"options" (object containing options used to create the collection).</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">MongoDB 2.6 and earlier versions require the <em>"name"</em> criteria, if specified, to be a string value (i.e. equality match). This is because the driver must prefix the value with the database name in order to query the <em>system.namespaces</em> collection. Later versions of MongoDB do not have this limitation, as the driver will use the listCollections command.</span></p></blockquote></li> <li class="listitem"><p class="para"><em>"includeSystemCollections"</em></p><p class="para">Boolean, defaults to <strong><code>FALSE</code></strong>. Determines whether system collections should be included in the result.</p></li> </ul> </p> <p class="para"> The following option may be used with MongoDB 2.8+: <ul class="itemizedlist"> <li class="listitem"><p class="para"><em>"maxTimeMS"</em></p><p class="para">Specifies a cumulative time limit in milliseconds for processing the operation on the server (does not include idle time). If the operation is not completed by the server within the timeout period, a <a href="class.mongoexecutiontimeoutexception.html" class="classname">MongoExecutionTimeoutException</a> will be thrown.</p></li> </ul> </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-mongodb.getcollectionnames-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the collection names as an array of strings. </p> </div> <div class="refsect1 errors" id="refsect1-mongodb.getcollectionnames-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> For MongoDB 2.6 and earlier, <a href="class.mongoexception.html" class="classname">MongoException</a> will be thrown if a non-string value was specified for the <em>"filter"</em> option's <em>"name"</em> criteria. </p> </div> <div class="refsect1 changelog" id="refsect1-mongodb.getcollectionnames-changelog"> <h3 class="title">Changelog</h3> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>1.6.0</td> <td> Changed first parameter to be an array of options. Pre-1.6.0, the first parameter was a boolean indicating the <em>"includeSystemCollections"</em> option. </td> </tr> </tbody> </table> </div> <div class="refsect1 examples" id="refsect1-mongodb.getcollectionnames-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1552"> <p><strong>Example #1 <span class="function"><strong>MongoDB::getCollectionNames()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$db </span><span style="color: #007700">= </span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">selectDB</span><span style="color: #007700">(</span><span style="color: #DD0000">"demo"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collections </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">getCollectionNames</span><span style="color: #007700">();<br /><br />foreach (</span><span style="color: #0000BB">$collections </span><span style="color: #007700">as </span><span style="color: #0000BB">$collectionName</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Found collection: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$collectionName</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> ... Found collection: img Found collection: beer Found collection: collation ... </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-mongodb.getcollectionnames-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="mongodb.listcollections.html" class="function" rel="rdfs-seeAlso">MongoDB::listCollections()</a> - Gets an array of MongoCollection objects for all collections in this database</span></li> <li class="member"><span class="function"><a href="mongodb.getcollectioninfo.html" class="function" rel="rdfs-seeAlso">MongoDB::getCollectionInfo()</a> - Returns information about collections in this database</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb.getcollectioninfo.html">MongoDB::getCollectionInfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb.getdbref.html">MongoDB::getDBRef</a></div> <div class="up"><a href="class.mongodb.html">MongoDB</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>