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