Skip to content

Commit a772b6a

Browse files
authored
Merge pull request #141 from rustagir/DOCSP-41985-compound-idx
DOCSP-41985: compound idx
2 parents 668f3dc + a497539 commit a772b6a

File tree

5 files changed

+113
-11
lines changed

5 files changed

+113
-11
lines changed

source/includes/indexes/indexes.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@
3131
echo json_encode($document), PHP_EOL;
3232
// end-index-single-query
3333

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
47+
3448
// start-multikey
3549
$indexName = $collection->createIndex(['cast' => 1]);
3650
// end-multikey

source/indexes.txt

Lines changed: 11 additions & 9 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
/indexes/multikey-index
2829

2930
Overview
@@ -32,11 +33,11 @@ Overview
3233
On this page, you can see copyable code examples that show how to manage
3334
different types of indexes by using the {+php-library+}.
3435

35-
.. .. tip::
36-
..
37-
.. To learn more about working with indexes, see the :ref:`php-work-indexes`
38-
.. guide. To learn more about any of the indexes shown on this page, see the link
39-
.. provided in each section.
36+
.. tip::
37+
38+
To learn more about working with indexes, see the :ref:`php-index-mgmt`
39+
guide. To learn more about any of the indexes shown on this page, see the link
40+
provided in each section.
4041

4142
To use an example from this page, copy the code example into the
4243
:ref:`sample application <php-index-sample>` or your own application.
@@ -77,8 +78,8 @@ The following example creates an ascending index on the specified field:
7778
:copyable:
7879
:dedent:
7980

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

8384
Compound Index
8485
--------------
@@ -93,7 +94,8 @@ on the specified fields:
9394
:copyable:
9495
:dedent:
9596

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

98100
Multikey Index
99101
--------------
@@ -298,4 +300,4 @@ The following example deletes an Atlas Search index with the specified name:
298300
:dedent:
299301

300302
.. To learn more about deleting search indexes, see the :ref:`php-atlas-search-index-drop`
301-
.. guide.
303+
.. guide.

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 compound 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,9 +67,9 @@ 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
- :ref:`php-multikey-index`
7172

72-
.. - :ref:`php-compound-index`
7373
.. - :ref:`php-atlas-search-index`
7474

7575
List Indexes

source/indexes/single-field-index.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Additional Information
8686
To view runnable examples that demonstrate how to manage indexes, see
8787
:ref:`php-indexes`.
8888

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

9292
API Documentation

0 commit comments

Comments
 (0)