Skip to content

Commit 8a2d3e9

Browse files
authored
DOCSP-45205 Multikey Indexes (#103)
* DOCSP-45205 Multikey Indexes * first draft * edits * remove quotes from key name * remove comments from code snippets
1 parent 96c104d commit 8a2d3e9

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

source/includes/indexes/multikey.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'mongo'
2+
3+
# Replace the placeholders with your credentials
4+
uri = "<connection string>"
5+
6+
# Sets the server_api field of the options object to Stable API version 1
7+
options = { server_api: { version: "1" }}
8+
9+
# Creates a new client and connect to the server
10+
client = Mongo::Client.new(uri, options)
11+
12+
# start-sample-data
13+
database = client.use('sample_mflix')
14+
collection = database[:movies]
15+
# end-sample-data
16+
17+
# Creates an index on the "cast" field
18+
# start-index-multikey
19+
collection.indexes.create_one({ cast: 1 })
20+
# end-index-multikey
21+
22+
# Finds a document with the specified cast members by using the newly created index
23+
# start-index-multikey-query
24+
filter = { cast: { '$all' => ['Aamir Khan', 'Kajol'] } }
25+
doc = collection.find(filter).first
26+
27+
if doc
28+
puts doc.to_json
29+
else
30+
puts "No document found"
31+
end
32+
# end-index-multikey-query
33+
34+
# Lists all indexes on the collection
35+
# start-check-multikey-index
36+
puts collection.indexes.collect(&:to_json)
37+
# end-check-multikey-index

source/indexes.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Optimize Queries by Using Indexes
2121
.. toctree::
2222
:titlesonly:
2323
:maxdepth: 1
24-
24+
2525
Single Field </indexes/single-field-index>
2626
Compound </indexes/compound-index>
27-
.. Multikey </indexes/multikey-index>
27+
Multikey </indexes/multikey-index>
2828
.. Atlas Search </indexes/atlas-search-index>
2929

3030
Overview

source/indexes/multikey-index.txt

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
.. _ruby-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 ``create_one`` method and the same syntax that you use to create
26+
a :ref:`single field index <ruby-single-field-index>`.
27+
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`` variables:
44+
45+
.. literalinclude:: /includes/indexes/single-field.rb
46+
:start-after: start-sample-data
47+
:end-before: end-sample-data
48+
:language: ruby
49+
:copyable:
50+
51+
To learn how to create a free MongoDB Atlas cluster and
52+
load the sample datasets, see the :atlas:`Get Started with Atlas
53+
</getting-started>` guide.
54+
55+
Create a Multikey Index
56+
-----------------------
57+
58+
Use the ``create_one`` method to create a multikey index. The following example
59+
creates an index in ascending order on the ``cast`` field:
60+
61+
.. literalinclude:: /includes/indexes/multikey.rb
62+
:start-after: start-index-multikey
63+
:end-before: end-index-multikey
64+
:language: ruby
65+
:copyable:
66+
67+
Verify Index Creation
68+
---------------------
69+
70+
You can verify that the index was created by listing the indexes in the
71+
collection. You should see an index for ``cast`` in the list, as shown
72+
in the following output:
73+
74+
.. io-code-block::
75+
:copyable: true
76+
77+
.. input:: /includes/indexes/multikey.rb
78+
:start-after: start-check-multikey-index
79+
:end-before: end-check-multikey-index
80+
:language: ruby
81+
82+
.. output::
83+
:visible: true
84+
85+
{"v": 2, "key": {"cast": 1}, "name": "cast_1"}
86+
87+
Example Query
88+
-------------
89+
90+
The following is an example of a query that is covered by the index
91+
created on the ``cast`` field:
92+
93+
.. io-code-block::
94+
:copyable: true
95+
96+
.. input:: /includes/indexes/multikey.rb
97+
:start-after: start-index-multikey-query
98+
:end-before: end-index-multikey-query
99+
:language: ruby
100+
101+
.. output::
102+
:visible: false
103+
104+
{"_id":...,"title":"Fanaa",...,"cast": ["Aamir Khan", "Kajol", "Rishi Kapoor", "Tabu"],...}
105+
106+
Additional Information
107+
----------------------
108+
109+
To view runnable examples that demonstrate how to manage indexes, see
110+
:ref:`ruby-indexes`.
111+
112+
To learn more about multikey indexes, see :manual:`Multikey
113+
Indexes </core/indexes/index-types/index-multikey/>` in the {+mdb-server+} manual.
114+
115+
API Documentation
116+
~~~~~~~~~~~~~~~~~
117+
118+
To learn more about any of the methods discussed in this guide, see the
119+
following API documentation:
120+
121+
- `indexes <{+api-root+}/Mongo/Collection.html#indexes-instance_method>`__
122+
- `create_one <{+api-root+}/Mongo/Index/View.html>`__
123+
- `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__

0 commit comments

Comments
 (0)