php-doc-en/reference/mongo/mongocollection/deleteindex.xml
2014-05-28 15:37:34 +00:00

170 lines
4.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongocollection.deleteindex" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::deleteIndex</refname>
<refpurpose>Deletes an index from this collection</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>array</type><methodname>MongoCollection::deleteIndex</methodname>
<methodparam><type>string|array</type><parameter>keys</parameter></methodparam>
</methodsynopsis>
<para>
This method is identical to:
</para>
<programlisting role="php">
<![CDATA[
<?php
public function deleteIndexes($keys) {
$indexName = $this->toIndexString($keys);
return $this->db->command(array(
"deleteIndexes" => $this->getName(),
"index" => $indexName,
));
}
?>
]]>
</programlisting>
<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 <function>MongoCollection::createIndex</function>'s
<literal>"name"</literal> option).
</para>
<para>
Unfortunately, <function>MongoCollection::deleteIndex</function> 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 <literal>"x_1"</literal> would be used for the
argument <literal>"x"</literal>). If an array or object is provided, an index
name is generated just as if that argument was passed to
<function>MongoCollection::createIndex</function>.
</para>
<para>
In order to delete a custom-named index with the PHP driver, the
<literal>deleteIndexes</literal> database command must be used. For instance,
an index named "myIndex" could be deleted with the PHP driver by running:
</para>
<programlisting role="php">
<![CDATA[
<?php
$db->command(array(
"deleteIndexes" => $collection->getName(),
"index" => "myIndex",
));
?>
]]>
</programlisting>
<para>
To determine the name of an index with the PHP driver, you can query the
<literal>system.indexes</literal> collection of a database and look for the
<literal>"name"</literal> field of each result. The <literal>"ns"</literal>
field will indicate the collection to which each index belongs.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>keys</parameter>
</term>
<listitem>
<para>
An array specifying the index's fields as its keys. For each field, the
value is either the index direction or
<link xlink:href="&url.mongodb.dochub.indexes.types;">index type</link>.
If specifying direction, specify <literal>1</literal> for ascending or
<literal>-1</literal> for descending.
</para>
<para>
If a string is provided, it is assumed to be the single field name in an
ascending index.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the database response.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoCollection::deleteIndex</function> example</title>
<para>
This example passes the function string and array parameters.
</para>
<programlisting role="php">
<![CDATA[
<?php
$m = new MongoClient();
$c = $m->example->indices;
// create and remove a simple index
$c->createIndex(array("i"=>1));
$c->deleteIndex("i");
// create and remove a multi-key index
$c->ensureIndex(array("j" => 1, "k" => 1));
$c->deleteIndex(array("j" => 1, "k" => 1));
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><methodname>MongoCollection::createIndex</methodname></member>
<member><methodname>MongoCollection::deleteIndexes</methodname></member>
<member><methodname>MongoCollection::toIndexString</methodname></member>
<member>
The MongoDB
<link xlink:href="&url.mongodb.dochub.indexes;">index</link> and
<link xlink:href="&url.mongodb.dochub.indexes.types;">index type</link>
documentation.
</member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->