<!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>Counts the number of documents in this collection</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.construct.html">MongoCollection::__construct</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocollection.createdbref.html">MongoCollection::createDBRef</a></div> <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongocollection.count" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCollection::count</h1> <p class="verinfo">(PECL mongo >=0.9.0)</p><p class="refpurpose"><span class="refname">MongoCollection::count</span> — <span class="dc-title">Counts the number of documents in this collection</span></p> </div> <div class="refsect1 description" id="refsect1-mongocollection.count-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">int</span> <span class="methodname"><strong>MongoCollection::count</strong></span> ([ <span class="methodparam"><span class="type">array</span> <code class="parameter">$query</code><span class="initializer"> = array()</span></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = array()</span></span> ]] )</div> </div> <div class="refsect1 parameters" id="refsect1-mongocollection.count-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">query</code> </dt> <dd> <p class="para"> Associative array or object with fields to match. </p> </dd> <dt> <code class="parameter">options</code> </dt> <dd> <p class="para"> An array of options for the index creation. Currently available options include: <table class="doctable informaltable"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td><var class="varname"><var class="varname">hint</var></var></td> <td><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></td> <td> <p class="para"> Index to use for the query. If a string is passed, it should correspond to an index name. If an array or object is passed, it should correspond to the specification used to create the index (i.e. the first argument to <span class="function"><a href="mongocollection.createindex.html" class="function">MongoCollection::createIndex()</a></span>). </p> <span class="simpara">This option is only supported in MongoDB 2.6+.</span> </td> </tr> <tr> <td><var class="varname"><var class="varname">limit</var></var></td> <td><span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span></td> <td>The maximum number of matching documents to return.</td> </tr> <tr> <td><var class="varname"><var class="varname">maxTimeMS</var></var></td> <td><span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span></td> <td> <p class="para"> Specifies a cumulative time limit in milliseconds for processing the operation (does not include idle time). If the operation is not completed within the timeout period, a <a href="class.mongoexecutiontimeoutexception.html" class="classname">MongoExecutionTimeoutException</a> will be thrown. </p> <span class="simpara">This option is only supported in MongoDB 2.6+.</span> </td> </tr> <tr> <td><var class="varname"><var class="varname">skip</var></var></td> <td><span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span></td> <td>The number of matching documents to skip before returning results.</td> </tr> </tbody> </table> </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongocollection.count-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the number of documents matching the query. </p> </div> <div class="refsect1 errors" id="refsect1-mongocollection.count-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Throws <a href="class.mongoresultexception.html" class="classname">MongoResultException</a> if the server could not execute the command due to an error. </p> <p class="para"> Throws <a href="class.mongoexecutiontimeoutexception.html" class="classname">MongoExecutionTimeoutException</a> if command execution was terminated due to <var class="varname"><var class="varname">maxTimeMS</var></var>. </p> </div> <div class="refsect1 changelog" id="refsect1-mongocollection.count-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>1.6.0</td> <td> The second parameter is now an <code class="parameter">options</code> array. Passing <code class="parameter">limit</code> and <code class="parameter">skip</code> as the second and third parameters, respectively, is deprecated. </td> </tr> <tr> <td>1.0.7</td> <td> Added <code class="parameter">limit</code> and <code class="parameter">skip</code> as second and third parameters, respectively. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-mongocollection.count-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1571"> <p><strong>Example #1 <span class="function"><strong>MongoCollection::count()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">=></span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">=></span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">=></span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">count</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">count</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">=></span><span style="color: #0000BB">1</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> int(3) int(1) </pre></div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.construct.html">MongoCollection::__construct</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocollection.createdbref.html">MongoCollection::createDBRef</a></div> <div class="up"><a href="class.mongocollection.html">MongoCollection</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>