@@ -25,3 +25,100 @@ on array-valued fields. You can create a multikey index on a collection
2525by using the ``create_one`` method and the same syntax that you use to create
2626a :ref:`single field index <ruby-single-field-index>`.
2727
28+
29+ When creating a multikey index, you must specify the following details:
30+
31+ - The fields on which to create the index
32+
33+ - The sort order for each field (ascending or descending)
34+
35+ Sample Data
36+ ~~~~~~~~~~~
37+
38+ The examples in this guide use the ``movies`` collection in the
39+ ``sample_mflix`` database from the :atlas:`Atlas sample datasets
40+ </sample-data>`. To access this collection from your {+language+}
41+ application, create a ``Mongo::Client`` object that connects to
42+ an Atlas cluster and assign the following values to your ``database``
43+ and ``collection``
44+ variables:
45+
46+ .. literalinclude:: /includes/indexes/single-field.rb
47+ :start-after: start-sample-data
48+ :end-before: end-sample-data
49+ :language: ruby
50+ :copyable:
51+
52+ To learn how to create a free MongoDB Atlas cluster and
53+ load the sample datasets, see the :atlas:`Get Started with Atlas
54+ </getting-started>` guide.
55+
56+ Create a Multikey Index
57+ -----------------------
58+
59+ Use the ``create_one`` method to create a multikey index. The following example
60+ creates an index in ascending order on the ``cast`` field:
61+
62+ .. literalinclude:: /includes/indexes/multikey.rb
63+ :start-after: start-index-multikey
64+ :end-before: end-index-multikey
65+ :language: ruby
66+ :copyable:
67+
68+ Verify Index Creation
69+ ---------------------
70+
71+ You can verify that the index was created by listing the indexes in the
72+ collection. You should see an index for ``cast`` in the list, as shown
73+ in the following output:
74+
75+ .. io-code-block::
76+ :copyable: true
77+
78+ .. input:: /includes/indexes/multikey.rb
79+ :start-after: start-check-multikey-index
80+ :end-before: end-check-multikey-index
81+ :language: ruby
82+
83+ .. output::
84+ :visible: true
85+
86+ {"v": 2, "key": {"cast": 1}, "name": "cast_1"}
87+
88+ Example Query
89+ -------------
90+
91+ The following is an example of a query that is covered by the index
92+ created on the ``cast`` field:
93+
94+ .. io-code-block::
95+ :copyable: true
96+
97+ .. input:: /includes/indexes/multikey.rb
98+ :start-after: start-index-multikey-query
99+ :end-before: end-index-multikey-query
100+ :language: ruby
101+
102+ .. output::
103+ :visible: false
104+
105+ {"_id":...,"title":"Fanaa",...,"cast": ["Aamir Khan", "Kajol", "Rishi Kapoor", "Tabu"],...}
106+
107+ Additional Information
108+ ----------------------
109+
110+ To view runnable examples that demonstrate how to manage indexes, see
111+ :ref:`ruby-indexes`.
112+
113+ To learn more about multikey indexes, see :manual:`Multikey
114+ Indexes </core/indexes/index-types/index-multikey/>` in the {+mdb-server+} manual.
115+
116+ API Documentation
117+ ~~~~~~~~~~~~~~~~~
118+
119+ To learn more about any of the methods discussed in this guide, see the
120+ following API documentation:
121+
122+ - `indexes <{+api-root+}/Mongo/Collection.html#indexes-instance_method>`__
123+ - `create_one <{+api-root+}/Mongo/Index/View.html>`__
124+ - `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__
0 commit comments