Skip to content

Commit 1ef45ef

Browse files
committed
first draft
1 parent 00794c9 commit 1ef45ef

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
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+
# start-index-multikey
18+
# Creates an index on the "title" field
19+
collection.indexes.create_one({ cast: 1 })
20+
# end-index-multikey
21+
22+
# start-index-multikey-query
23+
# Finds a document with the specified cast members by using the newly created index
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+
# start-check-multikey-index
35+
# Lists all indexes on the collection
36+
puts collection.indexes.collect(&:to_json)
37+
# end-check-multikey-index

source/indexes/multikey-index.txt

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,100 @@ on array-valued fields. You can create a multikey index on a collection
2525
by using the ``create_one`` method and the same syntax that you use to create
2626
a :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

Comments
 (0)