<!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>Deletes an index from this collection</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.createdbref.html">MongoCollection::createDBRef</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocollection.deleteindexes.html">MongoCollection::deleteIndexes</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.deleteindex" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCollection::deleteIndex</h1> <p class="verinfo">(PECL mongo >=0.9.0)</p><p class="refpurpose"><span class="refname">MongoCollection::deleteIndex</span> — <span class="dc-title">Deletes an index from this collection</span></p> </div> <div class="refsect1 description" id="refsect1-mongocollection.deleteindex-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>MongoCollection::deleteIndex</strong></span> ( <span class="methodparam"><span class="type"><span class="type string|array">string|array</span></span> <code class="parameter">$keys</code></span> )</div> <p class="para rdfs-comment"> This method is identical to: </p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">public function </span><span style="color: #0000BB">deleteIndexes</span><span style="color: #007700">(</span><span style="color: #0000BB">$keys</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// toIndexString is a protected method that turns strings, arrays, and objs <br /> //into index names<br /> </span><span style="color: #0000BB">$index </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">toIndexString</span><span style="color: #007700">(</span><span style="color: #0000BB">$keys</span><span style="color: #007700">);<br /><br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">db</span><span style="color: #007700">-></span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"deleteIndexes" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getName</span><span style="color: #007700">(), <br /> </span><span style="color: #DD0000">"index" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$index</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> Each index, when created, is given a unique name. This is generally user-set (with <span class="function"><a href="mongocollection.ensureindex.html" class="function">MongoCollection::ensureIndex()</a></span>'s "name" option) or generated by the driver from a combination of key names and directions. This name is then used by <span class="function"><strong>MongoCollection::deleteIndex()</strong></span> to remove the function. </p> <p class="para"> Unfortunately, the <span class="function"><a href="mongocollection.ensureindex.html" class="function">MongoCollection::ensureIndex()</a></span> generates slightly different names than the shell and, due to backwards compatibility issues, <span class="function"><strong>MongoCollection::deleteIndex()</strong></span> cannot delete custom-named indexes as well. Thus, the best way to delete indexes created in the shell or with custom names is to directly call the <em>deleteIndexes</em> database command. </p> <p class="para"> Thus, if you named an index "superfast query", you could only delete it with the PHP driver by running: </p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$db</span><span style="color: #007700">-></span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"deleteIndexes" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">getName</span><span style="color: #007700">(), </span><span style="color: #DD0000">"index" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"superfast query"</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> To find what an index is named, you can query the <em>system.indexes</em> collection of a database and look for the <em>name</em> field. </p> </div> <div class="refsect1 parameters" id="refsect1-mongocollection.deleteindex-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"> <em><code class="parameter">keys</code></em> </span> <dd> <p class="para"> Field or fields from which to delete the index. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongocollection.deleteindex-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the database response. </p> </div> <div class="refsect1 examples" id="refsect1-mongocollection.deleteindex-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1438"> <p><strong>Example #1 <span class="function"><strong>MongoCollection::deleteIndex()</strong></span> example</strong></p> <div class="example-contents"><p> This example passes the function string and array parameters. </p></div> <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">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">example</span><span style="color: #007700">-></span><span style="color: #0000BB">indices</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// create an index<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">ensureIndex</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"i"</span><span style="color: #007700">=></span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// remove a simple index<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">deleteIndex</span><span style="color: #007700">(</span><span style="color: #DD0000">"i"</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">// create a multi-key index<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">ensureIndex</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"j" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"k" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// remove a multi-key index<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">deleteIndex</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"j" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"k" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></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.createdbref.html">MongoCollection::createDBRef</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocollection.deleteindexes.html">MongoCollection::deleteIndexes</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>