Skip to content

Commit a040fa8

Browse files
authored
Merge pull request #140 from rustagir/DOCSP-41986-multikey-indexes
DOCSP-41986: multikey indexes
2 parents 1f70e6d + 60f5c9c commit a040fa8

File tree

6 files changed

+114
-9
lines changed

6 files changed

+114
-9
lines changed

source/includes/indexes/indexes.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,16 @@
2828

2929
// start-index-single-query
3030
$document = $collection->findOne(['title' => 'Sweethearts']);
31-
echo json_encode($document) , PHP_EOL;
31+
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/includes/usage-examples/index-code-examples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function toJSON(object $document): string
3838

3939
// start-search-list
4040
foreach ($collection->listSearchIndexes() as $indexInfo) {
41-
echo toJSON($indexInfo) , PHP_EOL;
41+
echo toJSON($indexInfo), PHP_EOL;
4242
}
4343
// end-search-list
4444

source/indexes.txt

Lines changed: 3 additions & 2 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
--------
@@ -107,8 +108,8 @@ specified array-valued field:
107108
:copyable:
108109
:dedent:
109110

110-
.. TODO To learn more about multikey indexes, see the :ref:`php-multikey-index`
111-
.. guide.
111+
To learn more about multikey indexes, see the :ref:`php-multikey-index`
112+
guide.
112113

113114
Geospatial Index
114115
----------------

source/indexes/index-mgmt.txt

Lines changed: 4 additions & 3 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`
@@ -134,6 +135,6 @@ API Documentation
134135
To learn more about any of the methods or types discussed in this
135136
guide, see the following API documentation:
136137

137-
- `MongoDB\\Collection::listIndexes() <{+api+}/method/MongoDBCollection-listIndexes/>`__
138-
- `MongoDB\\Collection::dropIndex() <{+api+}/method/MongoDBCollection-dropIndex/>`__
139-
- `MongoDB\\Collection::dropIndexes() <{+api+}/method/MongoDBCollection-dropIndexes/>`__
138+
- :phpmethod:`MongoDB\Collection::listIndexes()`
139+
- :phpmethod:`MongoDB\Collection::dropIndex()`
140+
- :phpmethod:`MongoDB\Collection::dropIndexes()`

source/indexes/multikey-index.txt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 :ref:`single field
27+
<php-single-field-index>` or compound indexes.
28+
29+
Sample Data
30+
~~~~~~~~~~~
31+
32+
The examples in this guide use the ``movies`` collection in the
33+
``sample_mflix`` database from the :atlas:`Atlas sample datasets
34+
</sample-data>`. To learn how to create a free MongoDB Atlas cluster and
35+
load the sample datasets, see the :atlas:`Get Started with Atlas
36+
</getting-started>` guide.
37+
38+
Create a Multikey Index
39+
-----------------------
40+
41+
Use the ``MongoDB\Collection::createIndex()`` method to create a
42+
multikey index. The following example creates an index in ascending
43+
order on the array-valued ``cast`` field:
44+
45+
.. literalinclude:: /includes/indexes/indexes.php
46+
:start-after: start-multikey
47+
:end-before: end-multikey
48+
:language: php
49+
:copyable:
50+
:dedent:
51+
52+
The following is an example of a query that is covered by the index
53+
created in the preceding code example:
54+
55+
.. io-code-block::
56+
:copyable: true
57+
58+
.. input:: /includes/indexes/indexes.php
59+
:start-after: start-index-array-query
60+
:end-before: end-index-array-query
61+
:language: php
62+
:dedent:
63+
64+
.. output::
65+
:visible: false
66+
67+
{"_id":...,"title":"Holi",...,"cast":["Ashutosh Gowariker",
68+
"Aamir Khan","Rahul Ranade","Sanjeev Gandhi"],...}
69+
70+
Additional Information
71+
----------------------
72+
73+
Multikey indexes behave differently from other indexes in terms of query
74+
coverage, index bound computation, and sort behavior. To learn more
75+
about the behavior and limitations of multikey indexes, see
76+
:manual:`Multikey Indexes </core/index-multikey>` in the {+mdb-server+}
77+
manual.
78+
79+
To view runnable examples that demonstrate how to manage indexes, see
80+
:ref:`php-indexes`.
81+
82+
API Documentation
83+
~~~~~~~~~~~~~~~~~
84+
85+
To learn more about any of the methods discussed in this guide, see the following API
86+
documentation:
87+
88+
- :phpmethod:`MongoDB\Collection::createIndex()`
89+
- :phpmethod:`MongoDB\Collection::findOne()`

source/indexes/single-field-index.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ created in the preceding code example:
8383
Additional Information
8484
----------------------
8585

86+
To view runnable examples that demonstrate how to manage indexes, see
87+
:ref:`php-indexes`.
88+
8689
To learn more about single-field indexes, see :manual:`Single Field
8790
Indexes </core/index-single>` in the {+mdb-server+} manual.
8891

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

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

0 commit comments

Comments
 (0)