Skip to content

DOCSP-41985: compound idx #141

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
14 changes: 14 additions & 0 deletions source/includes/indexes/indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
echo json_encode($document), PHP_EOL;
// end-index-single-query

// start-index-compound
$indexName = $collection->createIndex(
['title' => 1, 'year' => 1]
);
// end-index-compound

// start-compound-query
$document = $collection->findOne(
['title' => ['$regex' => 'Sunrise'],
'year' => ['$gte' => 1990]]
);
echo json_encode($document) , PHP_EOL;
// end-compound-query

// start-multikey
$indexName = $collection->createIndex(['cast' => 1]);
// end-multikey
Expand Down
20 changes: 11 additions & 9 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/compound-index
/indexes/multikey-index

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

.. .. tip::
..
.. To learn more about working with indexes, see the :ref:`php-work-indexes`
.. guide. To learn more about any of the indexes shown on this page, see the link
.. provided in each section.
.. tip::

To learn more about working with indexes, see the :ref:`php-index-mgmt`
guide. To learn more about any of the indexes shown on this page, see the link
provided in each section.

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

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

Compound Index
--------------
Expand All @@ -93,7 +94,8 @@ on the specified fields:
:copyable:
:dedent:

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

Multikey Index
--------------
Expand Down Expand Up @@ -298,4 +300,4 @@ The following example deletes an Atlas Search index with the specified name:
:dedent:

.. To learn more about deleting search indexes, see the :ref:`php-atlas-search-index-drop`
.. guide.
.. guide.
86 changes: 86 additions & 0 deletions source/indexes/compound-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
.. _php-compound-index:

================
Compound Indexes
================

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

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

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

Overview
--------

**Compound indexes** hold references to multiple
fields within a collection's documents, improving query and sort
performance. You can create a compound index on a collection
by using the ``MongoDB\Collection::createIndex()`` method and the same
syntax that you use to create :ref:`single field indexes
<php-single-field-index>`.

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 Compound Index
-----------------------

Use the ``MongoDB\Collection::createIndex()`` method to create a
compound index. The following example creates an index in ascending
order on the ``title`` and ``year`` fields:

.. literalinclude:: /includes/indexes/indexes.php
:start-after: start-index-compound
:end-before: end-index-compound
: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-compound-query
:end-before: end-compound-query
:language: php
:dedent:

.. output::
:visible: false

{"_id":...,"title":"Before Sunrise",...,"year":1995,...}

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

To learn more about compound indexes, see :manual:`Compound
Indexes </core/index-compound>` 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()`
2 changes: 1 addition & 1 deletion source/indexes/index-mgmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ 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-compound-index`
- :ref:`php-multikey-index`

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

List Indexes
Expand Down
2 changes: 1 addition & 1 deletion source/indexes/single-field-index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ 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
To learn more about single field indexes, see :manual:`Single Field
Indexes </core/index-single>` in the {+mdb-server+} manual.

API Documentation
Expand Down
Loading