Skip to content

DOCSP-41986: multikey indexes #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion source/includes/indexes/indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,16 @@

// start-index-single-query
$document = $collection->findOne(['title' => 'Sweethearts']);
echo json_encode($document) , PHP_EOL;
echo json_encode($document), PHP_EOL;
// end-index-single-query

// start-multikey
$indexName = $collection->createIndex(['cast' => 1]);
// end-multikey

// start-index-array-query
$document = $collection->findOne(
['cast' => ['$in' => ['Aamir Khan', 'Kajol']]]
);
echo json_encode($document), PHP_EOL;
// end-index-array-query
2 changes: 1 addition & 1 deletion source/includes/usage-examples/index-code-examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function toJSON(object $document): string

// start-search-list
foreach ($collection->listSearchIndexes() as $indexInfo) {
echo toJSON($indexInfo) , PHP_EOL;
echo toJSON($indexInfo), PHP_EOL;
}
// end-search-list

Expand Down
5 changes: 3 additions & 2 deletions source/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Optimize Queries by Using Indexes

/indexes/index-mgmt
/indexes/single-field-index
/indexes/multikey-index

Overview
--------
Expand Down Expand Up @@ -107,8 +108,8 @@ specified array-valued field:
:copyable:
:dedent:

.. TODO To learn more about multikey indexes, see the :ref:`php-multikey-index`
.. guide.
To learn more about multikey indexes, see the :ref:`php-multikey-index`
guide.

Geospatial Index
----------------
Expand Down
7 changes: 4 additions & 3 deletions source/indexes/index-mgmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The following pages describe different index types and provide sample
code to programmatically create each type of index.

- :ref:`php-single-field-index`
- :ref:`php-multikey-index`

.. - :ref:`php-compound-index`
.. - :ref:`php-atlas-search-index`
Expand Down Expand Up @@ -134,6 +135,6 @@ API Documentation
To learn more about any of the methods or types discussed in this
guide, see the following API documentation:

- `MongoDB\\Collection::listIndexes() <{+api+}/method/MongoDBCollection-listIndexes/>`__
- `MongoDB\\Collection::dropIndex() <{+api+}/method/MongoDBCollection-dropIndex/>`__
- `MongoDB\\Collection::dropIndexes() <{+api+}/method/MongoDBCollection-dropIndexes/>`__
- :phpmethod:`MongoDB\Collection::listIndexes()`
- :phpmethod:`MongoDB\Collection::dropIndex()`
- :phpmethod:`MongoDB\Collection::dropIndexes()`
89 changes: 89 additions & 0 deletions source/indexes/multikey-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.. _php-multikey-index:

================
Multikey Indexes
================

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: index, query, optimization, efficiency

Overview
--------

**Multikey indexes** are indexes that improve the performance of queries
on array-valued fields. You can create a multikey index on a collection
by using the ``MongoDB\Collection::createIndex()`` method and the same
syntax that you use to create :ref:`single field
<php-single-field-index>` or compound indexes.

Sample Data
~~~~~~~~~~~

The examples in this guide use the ``movies`` collection in the
``sample_mflix`` database from the :atlas:`Atlas sample datasets
</sample-data>`. To learn how to create a free MongoDB Atlas cluster and
load the sample datasets, see the :atlas:`Get Started with Atlas
</getting-started>` guide.

Create a Multikey Index
-----------------------

Use the ``MongoDB\Collection::createIndex()`` method to create a
multikey index. The following example creates an index in ascending
order on the array-valued ``cast`` field:

.. literalinclude:: /includes/indexes/indexes.php
:start-after: start-multikey
:end-before: end-multikey
:language: php
:copyable:
:dedent:

The following is an example of a query that is covered by the index
created in the preceding code example:

.. io-code-block::
:copyable: true

.. input:: /includes/indexes/indexes.php
:start-after: start-index-array-query
:end-before: end-index-array-query
:language: php
:dedent:

.. output::
:visible: false

{"_id":...,"title":"Holi",...,"cast":["Ashutosh Gowariker",
"Aamir Khan","Rahul Ranade","Sanjeev Gandhi"],...}

Additional Information
----------------------

Multikey indexes behave differently from other indexes in terms of query
coverage, index bound computation, and sort behavior. To learn more
about the behavior and limitations of multikey indexes, see
:manual:`Multikey Indexes </core/index-multikey>` in the {+mdb-server+}
manual.

To view runnable examples that demonstrate how to manage indexes, see
:ref:`php-indexes`.

API Documentation
~~~~~~~~~~~~~~~~~

To learn more about any of the methods discussed in this guide, see the following API
documentation:

- :phpmethod:`MongoDB\Collection::createIndex()`
- :phpmethod:`MongoDB\Collection::findOne()`
7 changes: 5 additions & 2 deletions source/indexes/single-field-index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ created in the preceding code example:
Additional Information
----------------------

To view runnable examples that demonstrate how to manage indexes, see
:ref:`php-indexes`.

To learn more about single-field indexes, see :manual:`Single Field
Indexes </core/index-single>` in the {+mdb-server+} manual.

Expand All @@ -92,5 +95,5 @@ API Documentation
To learn more about any of the methods discussed in this guide, see the following API
documentation:

- `MongoDB\\Collection::createIndex() <{+api+}/method/MongoDBCollection-createIndex/>`__
- `MongoDB\\Collection::findOne() <{+api+}/method/MongoDBCollection-findOne/>`__
- :phpmethod:`MongoDB\Collection::createIndex()`
- :phpmethod:`MongoDB\Collection::findOne()`
Loading