Skip to content

Commit 41acb2d

Browse files
committed
DOCSP-41141: Indexes (#13)
(cherry picked from commit 8920746)
1 parent 6f16dd3 commit 41acb2d

File tree

4 files changed

+352
-1
lines changed

4 files changed

+352
-1
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
77
]
88

99
toc_landing_pages = [
10-
"/read"
10+
"/read",
11+
"/indexes"
1112
]
1213

1314
[constants]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.example
2+
import com.mongodb.ConnectionString
3+
import com.mongodb.MongoClientSettings
4+
import com.mongodb.client.model.ClusteredIndexOptions
5+
import com.mongodb.client.model.Filters.*
6+
import com.mongodb.client.model.IndexOptions
7+
import com.mongodb.client.model.Indexes
8+
import com.mongodb.client.*
9+
import com.mongodb.kotlin.client.MongoClient
10+
import org.bson.Document
11+
12+
fun main() {
13+
val uri = "<connection string URI>"
14+
15+
val settings = MongoClientSettings.builder()
16+
.applyConnectionString(ConnectionString(uri))
17+
.retryWrites(true)
18+
.build()
19+
20+
val mongoClient = MongoClient.create(settings)
21+
val database = mongoClient.getDatabase("<database name>")
22+
val collection = database.getCollection<Document>("<collection name>")
23+
24+
// start-single-field
25+
collection.createIndex(Indexes.ascending("<field name"))
26+
// end-single-field
27+
28+
// start-compound
29+
collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"))
30+
// end-compound
31+
32+
// start-multikey
33+
collection.createIndex(Indexes.ascending("<array field name>"))
34+
// end-multikey
35+
36+
// start-search-create
37+
val index = Document("mappings", Document("dynamic", true))
38+
collection.createSearchIndex("<index name>", index)
39+
// end-search-create
40+
41+
// start-search-list
42+
val results = collection.listSearchIndexes()
43+
44+
results.forEach { result ->
45+
println(result)
46+
}
47+
// end-search-list
48+
49+
// start-search-update
50+
val newIndex = Document("mappings", Document("dynamic", true))
51+
collection.updateSearchIndex("<index name>", newIndex)
52+
// end-search-update
53+
54+
// start-search-delete
55+
collection.dropIndex("<index name>")
56+
// end-search-delete
57+
58+
// start-text
59+
collection.createIndex(Indexes.text("<field name>"))
60+
// end-text
61+
62+
// start-geo
63+
collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"))
64+
// end-geo
65+
66+
// start-unique
67+
val indexOptions = IndexOptions().unique(true)
68+
collection.createIndex(Indexes.ascending("<field name>"), indexOptions)
69+
// end-unique
70+
71+
// start-wildcard
72+
collection.createIndex(Indexes.ascending("$**"))
73+
// end-wildcard
74+
75+
// start-clustered
76+
val clusteredIndexOptions = ClusteredIndexOptions(
77+
Indexes.ascending("_id"),
78+
true
79+
)
80+
81+
val collection = database.createCollection("<collection name>", clusteredIndexOptions)
82+
// end-clustered
83+
84+
// start-remove
85+
collection.dropIndex("<index name>")
86+
// end-remove
87+
}

source/index.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
.. toctree::
1515

16+
/write-operations
1617
/read
18+
/indexes
1719
/faq
1820
/connection-troubleshooting
1921
/issues-and-help

source/indexes.txt

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
.. _kotlin-sync-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 Kotlin Sync driver.
19+
:keywords: query, optimization, efficiency, usage example, code example
20+
21+
.. TODO
22+
.. .. toctree::
23+
.. :titlesonly:
24+
.. :maxdepth: 1
25+
26+
.. /work-with-indexes
27+
28+
Overview
29+
--------
30+
31+
On this page, you can see copyable code examples that show how to manage different
32+
types of indexes by using the {+driver-short+}.
33+
34+
.. TODO
35+
.. .. tip::
36+
37+
.. To learn more about working with indexes, see the :ref:`kotlin-sync-work-with-indexes`
38+
.. guide. To learn more about any of the indexes shown on this page, see the link
39+
.. provided in each section.
40+
41+
To use an example from this page, copy the code example into the
42+
:ref:`sample application <kotlin-sync-index-sample>` or your own application.
43+
Be sure to replace all placeholders in the code examples, such as ``<connection string URI>``, with
44+
the relevant values for your MongoDB deployment.
45+
46+
.. _kotlin-sync-index-sample:
47+
48+
.. include:: /includes/usage-examples/sample-app-intro.rst
49+
50+
.. literalinclude:: /includes/usage-examples/sample-app.kt
51+
:language: kotlin
52+
:copyable:
53+
:linenos:
54+
:emphasize-lines: 20-22
55+
56+
Single Field Index
57+
------------------
58+
59+
The following example creates an ascending index on the specified field:
60+
61+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
62+
:start-after: start-single-field
63+
:end-before: end-single-field
64+
:language: kotlin
65+
:copyable:
66+
:dedent:
67+
68+
.. TODO: To learn more about single field indexes, see the
69+
.. :ref:`kotlin-sync-single-field-index` guide.
70+
71+
Compound Index
72+
--------------
73+
74+
The following example creates a compound index on the specified fields:
75+
76+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
77+
:start-after: start-compound
78+
:end-before: end-compound
79+
:language: kotlin
80+
:copyable:
81+
:dedent:
82+
83+
.. TODO: To learn more about compound indexes, see the :ref:`kotlin-sync-compound-index`
84+
.. guide.
85+
86+
Multikey Index
87+
--------------
88+
89+
The following example creates a multikey index on the specified array-valued field:
90+
91+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
92+
:start-after: start-multikey
93+
:end-before: end-multikey
94+
:language: kotlin
95+
:copyable:
96+
:dedent:
97+
98+
.. TODO To learn more about multikey indexes, see the :ref:`kotlin-sync-multikey-index`
99+
.. guide.
100+
101+
Geospatial Index
102+
----------------
103+
104+
The following example creates a 2dsphere index on the specified field that contains
105+
GeoJSON objects:
106+
107+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
108+
:start-after: start-geo
109+
:end-before: end-geo
110+
:language: kotlin
111+
:copyable:
112+
:dedent:
113+
114+
.. TODO: To learn more about geospatial indexes, see the :ref:`kotlin-sync-geospatial-index`
115+
.. guide.
116+
117+
Unique Index
118+
------------
119+
120+
The following example creates a unique index on the specified field:
121+
122+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
123+
:start-after: start-unique
124+
:end-before: end-unique
125+
:language: kotlin
126+
:copyable:
127+
:dedent:
128+
129+
.. TODO: To learn more about unique indexes, see the :ref:`kotlin-sync-unique-index`
130+
.. guide.
131+
132+
Wildcard Index
133+
--------------
134+
135+
The following example creates a wildcard index in the specified collection:
136+
137+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
138+
:start-after: start-wildcard
139+
:end-before: end-wildcard
140+
:language: kotlin
141+
:copyable:
142+
:dedent:
143+
144+
.. TODO: To learn more about wildcard indexes, see the :ref:`kotlin-sync-wildcard-index`
145+
.. guide.
146+
147+
Clustered Index
148+
---------------
149+
150+
The following example creates a new collection with a clustered index on the ``_id``
151+
field:
152+
153+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
154+
:start-after: start-clustered
155+
:end-before: end-clustered
156+
:language: kotlin
157+
:copyable:
158+
:dedent:
159+
160+
.. TODO: To learn more about wildcard indexes, see the :ref:`kotlin-sync-clustered-index`
161+
.. guide.
162+
163+
Atlas Search Index Management
164+
-----------------------------
165+
166+
The following sections contain code examples that describe how to manage Atlas Search
167+
indexes.
168+
169+
.. TODO: To learn more about Atlas search indexes, see the :ref:`kotlin-sync-atlas-search-index`
170+
.. guide.
171+
172+
Create Search Index
173+
~~~~~~~~~~~~~~~~~~~
174+
175+
The following example creates an Atlas search index on the specified field:
176+
177+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
178+
:start-after: start-search-create
179+
:end-before: end-search-create
180+
:language: kotlin
181+
:copyable:
182+
:dedent:
183+
184+
.. TODO: To learn more about creating serach indexes, see the :ref:`kotlin-sync-atlas-search-index-create`
185+
.. guide.
186+
187+
List Search Indexes
188+
~~~~~~~~~~~~~~~~~~~
189+
190+
The following example prints a list of Atlas search indexes in the specified collection:
191+
192+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
193+
:start-after: start-search-list
194+
:end-before: end-search-list
195+
:language: kotlin
196+
:copyable:
197+
:dedent:
198+
199+
.. TODO: To learn more about listing search indexes, see the :ref:`kotlin-sync-atlas-search-index-list`
200+
.. guide.
201+
202+
Update Search Indexes
203+
~~~~~~~~~~~~~~~~~~~~~
204+
205+
The following example updates an existing Atlas search index with the specified
206+
new index definition:
207+
208+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
209+
:start-after: start-search-update
210+
:end-before: end-search-update
211+
:language: kotlin
212+
:copyable:
213+
:dedent:
214+
215+
.. TODO: To learn more about updating search indexes, see the :ref:`kotlin-sync-atlas-search-index-update`
216+
.. guide.
217+
218+
Delete Search Indexes
219+
~~~~~~~~~~~~~~~~~~~~~
220+
221+
The following example deletes an Atlas search index with the specified name:
222+
223+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
224+
:start-after: start-search-delete
225+
:end-before: end-search-delete
226+
:language: kotlin
227+
:copyable:
228+
:dedent:
229+
230+
.. TODO: To learn more about deleting search indexes, see the :ref:`kotlin-sync-atlas-search-index-drop`
231+
.. guide.
232+
233+
Text Index
234+
----------
235+
236+
The following example creates a text index on the specified string field:
237+
238+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
239+
:start-after: start-text
240+
:end-before: end-text
241+
:language: kotlin
242+
:copyable:
243+
:dedent:
244+
245+
.. TODO: To learn more about text indexes, see the :ref:`kotlin-sync-text-index`
246+
.. guide.
247+
248+
Delete an Index
249+
---------------
250+
251+
The following example deletes an index with the specified name:
252+
253+
.. literalinclude:: /includes/usage-examples/index-code-examples.kt
254+
:start-after: start-remove
255+
:end-before: end-remove
256+
:language: kotlin
257+
:copyable:
258+
:dedent:
259+
260+
.. TODO: To learn more about removing indexes, see :ref:`kotlin-sync-indexes-remove`
261+
.. in the Work with Indexes guide.

0 commit comments

Comments
 (0)