Skip to content

Commit 0c42a80

Browse files
committed
DOCSP-41143: Compound Indexes
1 parent 12ef143 commit 0c42a80

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

source/includes/indexes/indexes.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ data class Movie(
1212
@BsonId
1313
val id: ObjectId,
1414
val title: String? = "",
15+
val type: String? = "",
1516
val genre: String? = "",
1617
val cast: List<String>? = null,
1718
val plot: String? = "",
@@ -51,4 +52,22 @@ fun main() {
5152
println(result)
5253
}
5354
// end-index-single-query
55+
56+
// start-index-compound
57+
collection.createIndex(Indexes.ascending(Movie::type.name, Movie::genres.name))
58+
// end-index-compound
59+
60+
// start-index-compound-query
61+
val filter = and(
62+
eq(Movie::type.name, "movie"),
63+
`in`(Movie::genres.name, "Drama")
64+
)
65+
val sort = Sorts.ascending(Movie::type.name, Movie::genres.name)
66+
val results = collection.find(filter).sort(sort)
67+
68+
results.forEach { result ->
69+
println(result)
70+
}
71+
// end-index-compound-query
72+
5473
}

source/indexes/compound-index.txt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
.. _kotlin-sync-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+
:manual:`Compound indexes </core/index-compound/>` hold references to multiple
24+
fields within a collection's documents, improving query and sort performance.
25+
26+
When creating a single-field index, you must specify the following:
27+
28+
- The fields on which to create the index
29+
- The sort order for each field (ascending or descending)
30+
31+
Sample Data
32+
~~~~~~~~~~~
33+
34+
The examples in this guide use the ``movies`` collection in the ``sample_mflix``
35+
database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
36+
free MongoDB Atlas cluster and load the sample datasets, see the
37+
:atlas:`Get Started with Atlas </getting-started>` guide.
38+
39+
The following {+language+} data class models the documents in this collection:
40+
41+
.. literalinclude:: /includes/indexes/indexes.kt
42+
:start-after: start-movie-class
43+
:end-before: end-movie-class
44+
:language: kotlin
45+
:copyable:
46+
47+
Create a Compound Index
48+
-----------------------
49+
50+
The following example creates a compound index on the ``type`` and ``genre`` fields:
51+
52+
.. literalinclude:: /includes/indexes/indexes.kt
53+
:start-after: start-index-compound
54+
:end-before: end-index-compound
55+
:language: kotlin
56+
:copyable:
57+
:dedent:
58+
59+
The following is an example of a query that uses the index created in
60+
the preceding code example:
61+
62+
.. io-code-block::
63+
:copyable: true
64+
65+
.. input:: /includes/indexes/indexes.kt
66+
:start-after: start-index-compound-query
67+
:end-before: end-index-compound-query
68+
:language: kotlin
69+
:dedent:
70+
71+
.. output::
72+
:visible: false
73+
74+
Movie(id=573a1392f29313caabcda755, title=China Seas, type=movie, genres=[Action, Drama, Adventure], ...)
75+
Movie(id=573a1392f29313caabcd9ca6, title=Scarface, type=movie, genres=[Action, Crime, Drama], ... )
76+
Movie(id=573a1392f29313caabcdb258, title=The Hurricane, type=movie, genres=[Action, Drama, Romance], ...)
77+
Movie(id=573a1391f29313caabcd820b, title=Beau Geste, type=movie, genres=[Action, Adventure, Drama], ...)
78+
...
79+
80+
81+
Additional Information
82+
----------------------
83+
84+
To learn more about compound indexes, see :manual:`Compound Indexes </core/index-compound>`
85+
in the {+mdb-server+} manual.
86+
87+
API Documentation
88+
~~~~~~~~~~~~~~~~~
89+
90+
To learn more about any of the methods or types discussed in this
91+
guide, see the following API Documentation:
92+
93+
- `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__
94+
- `filter() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/filter.html>`__
95+
- `sort() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/sort.html>`__

source/work-with-indexes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Work with Indexes
2020
.. toctree::
2121

2222
/indexes/single-field-index.txt
23+
/indexes/compound-index.txt
2324

2425
Overview
2526
--------

0 commit comments

Comments
 (0)