php-doc-en/reference/mongo/mongocollection/createindex.xml
2018-01-25 15:34:22 +00:00

248 lines
7.7 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongocollection.createindex" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::createIndex</refname>
<refpurpose>
Creates an index on the specified field(s) if it does not already exist
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>bool</type><methodname>MongoCollection::createIndex</methodname>
<methodparam><type>array</type><parameter>keys</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>array()</initializer></methodparam>
</methodsynopsis>
<para>
Creates an index on the specified field(s) if it does not already exist.
Fields may be indexed with a direction (e.g. ascending or descending) or a
special type (e.g. text, geospatial, hashed).
</para>
<note>
<para>
This method will use the
<link xlink:href="&url.mongodb.docs.command;createIndexes">createIndexes</link>
database command when communicating with MongoDB 2.6+. For previous database
versions, the method will perform an insert operation on the
special <literal>system.indexes</literal> collection.
</para>
</note>
</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>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>options</parameter>
</term>
<listitem>
<para>
An array of options for the index creation. We pass all given options
straight to the server, but a non-exhaustive list of currently
available options include:
<itemizedlist>
&mongo.index.parameters.unique;
&mongo.index.parameters.sparse;
&mongo.index.parameters.expireafterseconds;
&mongo.index.parameters.name;
&mongo.index.parameters.background;
&mongo.writes.parameters.sockettimeoutms;
</itemizedlist>
</para>
<para>
The following option may be used with MongoDB 2.6+:
<itemizedlist>
&mongo.command.parameters.maxtimems;
</itemizedlist>
</para>
<para>
The following options may be used with MongoDB versions before 2.8:
<itemizedlist>
&mongo.index.parameters.dropdups;
</itemizedlist>
</para>
<para>
The following options may be used with MongoDB versions before 2.6:
<itemizedlist>
&mongo.writes.parameters.writeconcern;
&mongo.writes.parameters.writeconcerntimeoutms;
</itemizedlist>
</para>
<para>
The following options are deprecated and should no longer be used:
<itemizedlist>
&mongo.writes.parameters.safe;
&mongo.writes.parameters.timeout;
&mongo.writes.parameters.writeconcerntimeout;
</itemizedlist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns an array containing the status of the index creation. The array
contains whether the operation succeeded (<literal>"ok"</literal>), the
number of indexes before and after the operation
(<literal>"numIndexesBefore"</literal> and
<literal>"numIndexesAfter"</literal>), and whether the collection that the
index belongs to has been created
(<literal>"createdCollectionAutomatically"</literal>). If the index already
existed and did not need to be created, a <literal>"note"</literal> field may
be present in lieu of <literal>"numIndexesAfter"</literal>.
</para>
<para>
With MongoDB 2.4 and earlier, a status document is only returned if the
<link linkend="mongo.writeconcerns">write concern</link> is at least
<literal>1</literal>. Otherwise, &true; is returned. The fields in the status
document are different, except for the <literal>"ok"</literal> field, which
signals whether the index creation was successful. Additional fields are
described in the documentation for
<function>MongoCollection::insert</function>.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws <classname>MongoException</classname> if the index name is longer
than 128 bytes, or if the index specification is not an array.
</para>
<para>
Throws <classname>MongoDuplicateKeyException</classname> if the server could not
create the unique index due to conflicting documents.
</para>
<para>
Throws <classname>MongoResultException</classname> if the server could not
create the index due to an error.
</para>
&mongo.errors.exceptions.writeconcern;
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoCollection::createIndex</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$c = new MongoCollection($db, 'foo');
// create an index on 'x' ascending
$c->createIndex(array('x' => 1));
// create a unique index on 'y'
$c->createIndex(array('y' => 1), array('unique' => true));
// create a compound index on 'za' ascending and 'zb' descending
$c->createIndex(array('za' => 1, 'zb' => -1));
?>
]]>
</programlisting>
</example>
<example>
<title>Geospatial Indexing</title>
<para>
Mongo supports geospatial indexes, which allow you to search for documents
near a given location or within a shape. The following example creates a
geospatial index on the <literal>"loc"</literal> field:
</para>
<programlisting role="php">
<![CDATA[
<?php
$collection->createIndex(array('loc' => '2dsphere'));
?>
]]>
</programlisting>
</example>
<example>
<title>Drop duplicates example</title>
<programlisting role="php">
<![CDATA[
<?php
$collection->insert(array('username' => 'joeschmoe'));
$collection->insert(array('username' => 'joeschmoe'));
/* Index creation fails, since you cannot create a unique index on a field when
* duplicates exist.
*/
$collection->createIndex(array('username' => 1), array('unique' => 1));
/* MongoDB will one of the conflicting documents and allow the unique index to
* be created.
*/
$collection->createIndex(array('username' => 1), array('unique' => 1, 'dropDups' => 1));
/* We now have a unique index and subsequent inserts with the same username will
* fail.
*/
$collection->insert(array('username' => 'joeschmoe'));
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><methodname>MongoCollection::deleteIndex</methodname></member>
<member><methodname>MongoCollection::deleteIndexes</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
-->