Skip to content

Commit 4fd1451

Browse files
committed
DOCSP-41986: multikey indexes
1 parent 1f70e6d commit 4fd1451

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

source/includes/indexes/indexes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@
3030
$document = $collection->findOne(['title' => 'Sweethearts']);
3131
echo json_encode($document) , PHP_EOL;
3232
// end-index-single-query
33+
34+
// start-multikey
35+
$indexName = $collection->createIndex(['cast' => 1]);
36+
// end-multikey
37+
38+
// start-index-array-query
39+
$document = $collection->findOne(
40+
['cast' => ['$in' => ['Aamir Khan', 'Kajol']]]
41+
);
42+
echo json_encode($document) , PHP_EOL;
43+
// end-index-array-query

source/indexes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Optimize Queries by Using Indexes
2424

2525
/indexes/index-mgmt
2626
/indexes/single-field-index
27+
/indexes/multikey-index
2728

2829
Overview
2930
--------

source/indexes/index-mgmt.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The following pages describe different index types and provide sample
6767
code to programmatically create each type of index.
6868

6969
- :ref:`php-single-field-index`
70+
- :ref:`php-multikey-index`
7071

7172
.. - :ref:`php-compound-index`
7273
.. - :ref:`php-atlas-search-index`

source/indexes/multikey-index.txt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
.. _php-multikey-index:
2+
3+
================
4+
Multikey Indexes
5+
================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: index, query, optimization, efficiency
19+
20+
Overview
21+
--------
22+
23+
**Multikey indexes** are indexes that improve the performance of queries
24+
on array-valued fields. You can create a multikey index on a collection
25+
by using the ``MongoDB\Collection::createIndex()`` method and the same
26+
syntax that you use to create single field or compound indexes.
27+
28+
Sample Data
29+
~~~~~~~~~~~
30+
31+
The examples in this guide use the ``movies`` collection in the
32+
``sample_mflix`` database from the :atlas:`Atlas sample datasets
33+
</sample-data>`. To learn how to create a free MongoDB Atlas cluster and
34+
load the sample datasets, see the :atlas:`Get Started with Atlas
35+
</getting-started>` guide.
36+
37+
Create a Multikey Index
38+
-----------------------
39+
40+
Use the ``MongoDB\Collection::createIndex()`` method to create a
41+
multikey index. The following example creates an index in ascending
42+
order on the array-valued ``cast`` field:
43+
44+
.. literalinclude:: /includes/indexes/indexes.php
45+
:start-after: start-multikey
46+
:end-before: end-multikey
47+
:language: php
48+
:copyable:
49+
:dedent:
50+
51+
The following is an example of a query that is covered by the index
52+
created in the preceding code example:
53+
54+
.. io-code-block::
55+
:copyable: true
56+
57+
.. input:: /includes/indexes/indexes.php
58+
:start-after: start-index-array-query
59+
:end-before: end-index-array-query
60+
:language: php
61+
:dedent:
62+
63+
.. output::
64+
:visible: false
65+
66+
{"_id":...,"title":"Holi",...,"cast":["Ashutosh Gowariker",
67+
"Aamir Khan","Rahul Ranade","Sanjeev Gandhi"],...}
68+
69+
Additional Information
70+
----------------------
71+
72+
Multikey indexes behave differently from other indexes in terms of query
73+
coverage, index bound computation, and sort behavior. To learn more
74+
about the behavior and limitations of multikey indexes, see
75+
:manual:`Multikey Indexes </core/index-multikey>` in the {+mdb-server+}
76+
manual.
77+
78+
API Documentation
79+
~~~~~~~~~~~~~~~~~
80+
81+
To learn more about any of the methods discussed in this guide, see the following API
82+
documentation:
83+
84+
- `MongoDB\\Collection::createIndex() <{+api+}/method/MongoDBCollection-createIndex/>`__
85+
- `MongoDB\\Collection::findOne() <{+api+}/method/MongoDBCollection-findOne/>`__

0 commit comments

Comments
 (0)