Skip to content

Commit f28a372

Browse files
committed
DOCSP-45204 Compound Index
1 parent 18b24a2 commit f28a372

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed

source/includes/indexes/compound.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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-compound
18+
# Creates an index on the "runtime" and "year" field
19+
collection.indexes.create_one({ runtime: -1, year: 1 })
20+
# end-index-compound
21+
22+
# start-index-compound-query
23+
# Finds a document with the specified runtime and release year
24+
# by using the newly created index
25+
filter = { '$and' => [
26+
{ 'runtime': { '$gt' => 90 } },
27+
{ 'year': { '$gt' => 2005 } }
28+
] }
29+
doc = collection.find(filter).first
30+
31+
if doc
32+
puts doc.to_json
33+
else
34+
puts "No document found"
35+
end
36+
# end-index-compound-query
37+
38+
# start-check-compound-index
39+
# Lists all indexes on the collection
40+
puts collection.indexes.collect(&:to_json)
41+
# end-check-compound-index

source/indexes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Optimize Queries by Using Indexes
2323
:maxdepth: 1
2424

2525
Single Field </indexes/single-field-index>
26-
.. Compound </indexes/compound-index>
26+
Compound </indexes/compound-index>
2727
.. Multikey </indexes/multikey-index>
2828
.. Atlas Search </indexes/atlas-search-index>
2929

source/indexes/compound-index.txt

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
.. ruby-compound-index:
2+
3+
================
4+
Compound 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+
**Compound indexes** hold references to multiple
24+
fields within a collection's documents, improving query and sort
25+
performance.
26+
27+
When creating a compound index, you must specify the following details:
28+
29+
- The fields on which to create the index
30+
31+
- The sort order for each field (ascending or descending)
32+
33+
Sample Data
34+
~~~~~~~~~~~
35+
36+
The examples in this guide use the ``movies`` collection in the
37+
``sample_mflix`` database from the :atlas:`Atlas sample datasets
38+
</sample-data>`. To access this collection from your {+language+}
39+
application, create a ``Mongo::Client`` object that connects to
40+
an Atlas cluster and assign the following values to your ``database``
41+
and ``collection``
42+
variables:
43+
44+
.. literalinclude:: /includes/indexes/single-field.rb
45+
:start-after: start-sample-data
46+
:end-before: end-sample-data
47+
:language: ruby
48+
:copyable:
49+
50+
To learn how to create a free MongoDB Atlas cluster and
51+
load the sample datasets, see the :atlas:`Get Started with Atlas
52+
</getting-started>` guide.
53+
54+
Create a Compound Index
55+
-----------------------
56+
57+
Use the ``create_one`` method to create a compound index. The following
58+
example creates an index in descending order on the ``runtime`` field and
59+
in ascending order on the ``year`` field:
60+
61+
.. literalinclude:: /includes/indexes/compound.rb
62+
:start-after: start-index-compound
63+
:end-before: end-index-compound
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 ``runtime`` and ``year`` in the list,
72+
as shown in the following output:
73+
74+
75+
.. io-code-block::
76+
:copyable: true
77+
78+
.. input:: /includes/indexes/compound.rb
79+
:start-after: start-check-compound-index
80+
:end-before: end-check-compound-index
81+
:language: ruby
82+
83+
.. output::
84+
:visible: true
85+
86+
{"v": 2, "key": {"runtime": -1, "year": 1}, "name": "runtime_-1_year_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 ``runtime`` and ``year`` fields:
93+
94+
.. io-code-block::
95+
:copyable: true
96+
97+
.. input:: /includes/indexes/compound.rb
98+
:start-after: start-index-compound-query
99+
:end-before: end-index-compound-query
100+
:language: ruby
101+
102+
.. output::
103+
:visible: false
104+
105+
{"_id":...,"runtime": 91,...,"title": "Monster House",...,"year": 2006,...}
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 compound indexes, see :manual:`Compound
114+
Indexes </core/index-compound>` 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)