| Server IP : 54.94.228.101 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/vendor/mongodb/mongodb/docs/tutorial/ |
Upload File : |
=======
Indexes
=======
.. default-domain:: mongodb
Indexes support the efficient execution of queries in MongoDB. Without indexes,
MongoDB must perform a *collection scan*, i.e. scan every document in a
collection, to select those documents that match the query statement. If an
appropriate index exists for a query, MongoDB can use the index to limit the
number of documents it must inspect.
The PHP driver supports managing indexes through the
:phpclass:`MongoDB\\Collection` class, which implements MongoDB's
cross-driver `Index Management
<https://github.com/mongodb/specifications/blob/master/source/index-management.rst>`_
and `Enumerating Indexes
<https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst>`_
specifications.
This document provides an introduction to creating, listing, and dropping
indexes using the |php-library|. The MongoDB Manual's :manual:`Indexes
</indexes>` reference provides more thorough information about indexing in
MongoDB.
Create Indexes
--------------
Create indexes with the :phpmethod:`MongoDB\\Collection::createIndex()` or
:phpmethod:`MongoDB\\Collection::createIndexes()` methods. Refer to the method
reference for more details about each method.
The following example creates an ascending index on the ``state`` field using
the :phpmethod:`createIndex() <MongoDB\\Collection::createIndex>` method:
.. code-block:: php
<?php
$collection = (new MongoDB\Client)->test->zips;
$result = $collection->createIndex(['state' => 1]);
var_dump($result);
When you create an index, the method returns its name, which is automatically
generated from its specification. The above example would output something
similar to::
string(7) "state_1"
List Indexes
------------
The :phpmethod:`MongoDB\\Collection::listIndexes()` method provides information
about the indexes in a collection. The
:phpmethod:`MongoDB\\Collection::listIndexes()` method returns an iterator of
:phpclass:`MongoDB\\Model\\IndexInfo` objects, which you can use to view
information about each index. Refer to the method reference for more details.
The following example lists all indexes in the ``zips`` collection in the
``test`` database:
.. code-block:: php
<?php
$collection = (new MongoDB\Client)->test->zips;
foreach ($collection->listIndexes() as $indexInfo) {
var_dump($indexInfo);
}
The output would resemble::
object(MongoDB\Model\IndexInfo)#10 (4) {
["v"]=>
int(1)
["key"]=>
array(1) {
["_id"]=>
int(1)
}
["name"]=>
string(4) "_id_"
["ns"]=>
string(9) "test.zips"
}
object(MongoDB\Model\IndexInfo)#13 (4) {
["v"]=>
int(1)
["key"]=>
array(1) {
["state"]=>
int(1)
}
["name"]=>
string(7) "state_1"
["ns"]=>
string(9) "test.zips"
}
Drop Indexes
------------
The :phpmethod:`MongoDB\\Collection::dropIndex()` method lets you drop a single
index while :phpmethod:`MongoDB\\Collection::dropIndexes()` drops all of the
indexes on a collection. Refer to the method reference for more details about
each method.
The following example drops a single index by its name, ``state_1``:
.. code-block:: php
<?php
$collection = (new MongoDB\Client)->test->zips;
$result = $collection->dropIndex('state_1');
var_dump($result);
The operation's output would resemble::
object(MongoDB\Model\BSONDocument)#11 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["nIndexesWas"]=>
int(2)
["ok"]=>
float(1)
}
}