Skip to content

Commit 18b24a2

Browse files
lindseymooremongoKartmcmorisi
authored
DOCSP-45203 Single Field Indexes (#100)
* add files * Update add-netlify-links.yml (#90) * DOCSP-45076: Document that count supports different filters from countDocuments * Test fix * Fix * Try again * Never mind * NR suggestion * DOCSP-45203 Single Field Indexes * Edits * edits * get rid of old commit work * code spacing * Stephanie's reiview * Tech review code improvements * update api links * add a space at eof * update listing description * add context to ex query code comment * style edit on prev --------- Co-authored-by: Mike Woofter <[email protected]> Co-authored-by: Michael Morisi <[email protected]>
1 parent 072c5d8 commit 18b24a2

File tree

5 files changed

+211
-2
lines changed

5 files changed

+211
-2
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title = "Ruby MongoDB Driver"
33
toc_landing_pages = [
44
"/get-started",
55
"/connect",
6-
"/write"
6+
"/write",
7+
"/indexes"
78
]
89

910
intersphinx = ["https://www.mongodb.com/docs/manual/objects.inv"]
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-single
18+
# Creates an index on the "title" field
19+
collection.indexes.create_one({ title: 1 })
20+
# end-index-single
21+
22+
# start-index-single-query
23+
# Finds a document with the title "Sweethearts" by using the newly created index
24+
filter = { title: 'Sweethearts' }
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-single-query
33+
34+
# start-check-single-index
35+
# Lists all indexes on the collection
36+
puts collection.indexes.collect(&:to_json)
37+
# end-check-single-index

source/index.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Connect </connect>
1717
Write Data </write>
1818
Read Data </read>
19+
Indexes </indexes>
1920
View the Source <https://github.com/mongodb/mongo-ruby-driver>
2021
API Documentation <{+api-root+}>
2122
Compatibility </compatibility>
@@ -24,7 +25,6 @@
2425
Databases & Collections </databases-collections>
2526
Write Data </write>
2627
Operations on Replica Sets </read-write-pref>
27-
Indexes </indexes>
2828
Monitor Your Application </monitoring>
2929
Data Aggregation </aggregation>
3030
Security </security>

source/indexes.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. _ruby-indexes:
2+
3+
=================================
4+
Optimize Queries by Using 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+
:description: Learn how to use indexes by using the MongoDB Scala Driver.
19+
:keywords: query, optimization, efficiency, usage example, code example
20+
21+
.. toctree::
22+
:titlesonly:
23+
:maxdepth: 1
24+
25+
Single Field </indexes/single-field-index>
26+
.. Compound </indexes/compound-index>
27+
.. Multikey </indexes/multikey-index>
28+
.. Atlas Search </indexes/atlas-search-index>
29+
30+
Overview
31+
--------
32+
33+
On this page, you can see copyable code examples that show how to manage
34+
different types of indexes by using the {+driver-long+}.
35+
36+
.. TODO

source/indexes/single-field-index.txt

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
.. _ruby-single-field-index:
2+
3+
====================
4+
Single Field 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+
Single field indexes are indexes with a reference to a single field of a
24+
document in a collection. These indexes improve single field query and
25+
sort performance. They also support :manual:`TTL Indexes </core/index-ttl>`
26+
that automatically remove documents from a collection after a certain
27+
amount of time or at a specified clock time.
28+
29+
When creating a single field index, you must specify the following
30+
details:
31+
32+
- The field on which to create the index
33+
- The sort order for the indexed values as either ascending or
34+
descending
35+
36+
.. note::
37+
38+
The default ``_id_`` index is an example of a single field index.
39+
This index is automatically created on the ``_id`` field when a new
40+
collection is created.
41+
42+
Sample Data
43+
~~~~~~~~~~~
44+
45+
The examples in this guide use the ``movies`` collection in the
46+
``sample_mflix`` database from the :atlas:`Atlas sample datasets
47+
</sample-data>`. To access this collection from your {+language+}
48+
application, create a ``Mongo::Client`` object that connects to
49+
an Atlas cluster and assign the following values to your ``database``
50+
and ``collection``
51+
variables:
52+
53+
.. literalinclude:: /includes/indexes/single-field.rb
54+
:start-after: start-sample-data
55+
:end-before: end-sample-data
56+
:language: ruby
57+
:copyable:
58+
59+
To learn how to create a free MongoDB Atlas cluster and
60+
load the sample datasets, see the :atlas:`Get Started with Atlas
61+
</getting-started>` guide.
62+
63+
Create a Single Field Index
64+
---------------------------
65+
66+
Use the ``create_one`` method to create a single
67+
field index. The following example creates an index in ascending order on the
68+
``title`` field:
69+
70+
.. literalinclude:: /includes/indexes/single-field.rb
71+
:start-after: start-index-single
72+
:end-before: end-index-single
73+
:language: ruby
74+
:copyable:
75+
76+
Verify Index Creation
77+
---------------------
78+
79+
You can verify that the index was created by listing the indexes in the
80+
collection. You should see an index for ``title`` in the list, as shown
81+
in the following output:
82+
83+
.. io-code-block::
84+
:copyable: true
85+
86+
.. input:: /includes/indexes/single-field.rb
87+
:start-after: start-check-single-index
88+
:end-before: end-check-single-index
89+
:language: ruby
90+
:dedent:
91+
92+
.. output::
93+
:visible: true
94+
95+
{"v": 2, "key": {"title": 1}, "name": "title_1"}
96+
97+
Example Query
98+
-------------
99+
100+
The following is an example of a query that is covered by the index
101+
created on the ``title`` field:
102+
103+
.. io-code-block::
104+
:copyable: true
105+
106+
.. input:: /includes/indexes/single-field.rb
107+
:start-after: start-index-single-query
108+
:end-before: end-index-single-query
109+
:language: ruby
110+
:dedent:
111+
112+
.. output::
113+
:visible: false
114+
115+
{"_id":...,"plot":"A musical comedy duo...",
116+
"genres":["Musical"],...,"title":"Sweethearts",...}
117+
118+
Additional Information
119+
----------------------
120+
121+
To view runnable examples that demonstrate how to manage indexes, see
122+
:ref:`ruby-indexes`.
123+
124+
To learn more about single field indexes, see :manual:`Single Field
125+
Indexes </core/index-single>` in the {+mdb-server+} manual.
126+
127+
API Documentation
128+
~~~~~~~~~~~~~~~~~
129+
130+
To learn more about any of the methods discussed in this guide, see the
131+
following API documentation:
132+
133+
- `indexes <{+api-root+}/Mongo/Collection.html#indexes-instance_method>`__
134+
- `create_one <{+api-root+}/Mongo/Index/View.html>`__
135+
- `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__

0 commit comments

Comments
 (0)