<!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.createindex.html">MongoCollection::createIndex</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: #0000BB">$indexName </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(<br /> </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">$indexName</span><span style="color: #007700">,<br /> ));<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> Each index is given a unique name when it is created. This is often generated by the driver based on the index key(s) and order/type, but custom names may also be specified with <span class="function"><a href="mongocollection.createindex.html" class="function">MongoCollection::createIndex()</a></span>'s <em>"name"</em> option). </p> <p class="para"> Unfortunately, <span class="function"><strong>MongoCollection::deleteIndex()</strong></span> cannot delete custom-named indexes due to a backwards compatibility issue. When a string argument is provided, it is assumed to be the single field name in an ascending index (e.g. the name <em>"x_1"</em> would be used for the argument <em>"x"</em>). If an array or object is provided, an index name is generated just as if that argument was passed to <span class="function"><a href="mongocollection.createindex.html" class="function">MongoCollection::createIndex()</a></span>. </p> <p class="para"> In order to delete a custom-named index with the PHP driver, the <em>deleteIndexes</em> database command must be used. For instance, an index named "myIndex" could be deleted 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(<br /> </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">(),<br /> </span><span style="color: #DD0000">"index" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"myIndex"</span><span style="color: #007700">,<br />));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> To determine the name of an index with the PHP driver, you can query the <em>system.indexes</em> collection of a database and look for the <em>"name"</em> field of each result. The <em>"ns"</em> field will indicate the collection to which each index belongs. </p> </div> <div class="refsect1 parameters" id="refsect1-mongocollection.deleteindex-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">keys</code> </dt> <dd> <p class="para"> An array specifying the index's fields as its keys. For each field, the value is either the index direction or <a href="https://docs.mongodb.com/manual/core/index-types/" class="link external">» index type</a>. If specifying direction, specify <em>1</em> for ascending or <em>-1</em> for descending. </p> <p class="para"> If a string is provided, it is assumed to be the single field name in an ascending index. </p> </dd> </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-1576"> <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 /><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 and remove a simple index<br /></span><span style="color: #0000BB">$c</span><span style="color: #007700">-></span><span style="color: #0000BB">createIndex</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 /></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 /></span><span style="color: #FF8000">// create and remove 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 /></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 /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-mongocollection.deleteindex-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"><span class="methodname"><a href="mongocollection.createindex.html" class="methodname" rel="rdfs-seeAlso">MongoCollection::createIndex()</a> - Creates an index on the specified field(s) if it does not already exist</span></li> <li class="member"><span class="methodname"><a href="mongocollection.deleteindexes.html" class="methodname" rel="rdfs-seeAlso">MongoCollection::deleteIndexes()</a> - Delete all indices for this collection</span></li> <li class="member"><span class="methodname"><a href="mongocollection.toindexstring.html" class="methodname" rel="rdfs-seeAlso">MongoCollection::toIndexString()</a> - Converts keys specifying an index to its identifying string</span></li> <li class="member"> The MongoDB <a href="https://docs.mongodb.com/manual/indexes/" class="link external">» index</a> and <a href="https://docs.mongodb.com/manual/core/index-types/" class="link external">» index type</a> documentation. </li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocollection.createindex.html">MongoCollection::createIndex</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>