Skip to content

Commit c0f6e1f

Browse files
committed
DOCSP-41985: compound idx
1 parent 1f70e6d commit c0f6e1f

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

source/includes/indexes/indexes.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,17 @@
3030
$document = $collection->findOne(['title' => 'Sweethearts']);
3131
echo json_encode($document) , PHP_EOL;
3232
// end-index-single-query
33+
34+
// start-index-compound
35+
$indexName = $collection->createIndex(
36+
['title' => 1, 'year' => 1]
37+
);
38+
// end-index-compound
39+
40+
// start-compound-query
41+
$document = $collection->findOne(
42+
['title' => ['$regex' => 'Sunrise'],
43+
'year' => ['$gte' => 1990]]
44+
);
45+
echo json_encode($document) , PHP_EOL;
46+
// end-compound-query

source/indexes.txt

Lines changed: 5 additions & 3 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/compound-index
2728

2829
Overview
2930
--------
@@ -76,8 +77,8 @@ The following example creates an ascending index on the specified field:
7677
:copyable:
7778
:dedent:
7879

79-
.. To learn more about single field indexes, see the
80-
.. :ref:`php-single-field-index` guide.
80+
To learn more about single field indexes, see the
81+
:ref:`php-single-field-index` guide.
8182

8283
Compound Index
8384
--------------
@@ -92,7 +93,8 @@ on the specified fields:
9293
:copyable:
9394
:dedent:
9495

95-
.. To learn more about compound indexes, see the :ref:`php-compound-index` guide.
96+
To learn more about compound indexes, see the :ref:`php-compound-index`
97+
guide.
9698

9799
Multikey Index
98100
--------------

source/indexes/compound-index.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.. _php-compound-index:
2+
3+
================
4+
Compound 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+
**Compound indexes** hold references to multiple
24+
fields within a collection's documents, improving query and sort
25+
performance. You can create a multikey index on a collection
26+
by using the ``MongoDB\Collection::createIndex()`` method and the same
27+
syntax that you use to create :ref:`single field indexes
28+
<php-single-field-index>`.
29+
30+
Sample Data
31+
~~~~~~~~~~~
32+
33+
The examples in this guide use the ``movies`` collection in the
34+
``sample_mflix`` database from the :atlas:`Atlas sample datasets
35+
</sample-data>`. To learn how to create a free MongoDB Atlas cluster and
36+
load the sample datasets, see the :atlas:`Get Started with Atlas
37+
</getting-started>` guide.
38+
39+
Create a Compound Index
40+
-----------------------
41+
42+
Use the ``MongoDB\Collection::createIndex()`` method to create a
43+
compound index. The following example creates an index in ascending
44+
order on the ``title`` and ``year`` fields:
45+
46+
.. literalinclude:: /includes/indexes/indexes.php
47+
:start-after: start-index-compound
48+
:end-before: end-index-compound
49+
:language: php
50+
:copyable:
51+
:dedent:
52+
53+
The following is an example of a query that is covered by the index
54+
created in the preceding code example:
55+
56+
.. io-code-block::
57+
:copyable: true
58+
59+
.. input:: /includes/indexes/indexes.php
60+
:start-after: start-compound-query
61+
:end-before: end-compound-query
62+
:language: php
63+
:dedent:
64+
65+
.. output::
66+
:visible: false
67+
68+
{"_id":...,"title":"Before Sunrise",...,"year":1995,...}
69+
70+
Additional Information
71+
----------------------
72+
73+
To learn more about compound indexes, see :manual:`Compound
74+
Indexes </core/index-compound>` in the {+mdb-server+} manual.
75+
76+
To view runnable examples that demonstrate how to manage indexes, see
77+
:ref:`php-indexes`.
78+
79+
API Documentation
80+
~~~~~~~~~~~~~~~~~
81+
82+
To learn more about any of the methods discussed in this guide, see the following API
83+
documentation:
84+
85+
- :phpmethod:`MongoDB\Collection::createIndex()`
86+
- :phpmethod:`MongoDB\Collection::findOne()`

source/indexes/index-mgmt.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ 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-compound-index`
7071

71-
.. - :ref:`php-compound-index`
7272
.. - :ref:`php-atlas-search-index`
7373

7474
List Indexes

source/indexes/single-field-index.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ created in the preceding code example:
8383
Additional Information
8484
----------------------
8585

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

8989
API Documentation

0 commit comments

Comments
 (0)