Skip to content

Commit 0379473

Browse files
authored
DOCSP-41143: Compound Indexes (#15)
1 parent 6dda8ff commit 0379473

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

source/includes/indexes/indexes.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ data class Movie(
1212
@BsonId
1313
val id: ObjectId,
1414
val title: String? = "",
15-
val genre: String? = "",
15+
val type: String? = "",
16+
val genres: List<String>? = null,
1617
val cast: List<String>? = null,
1718
val plot: String? = "",
1819
)
@@ -51,4 +52,21 @@ 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
5472
}

source/indexes/compound-index.txt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 compound 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, with
51+
both fields indexed in ascending order:
52+
53+
.. literalinclude:: /includes/indexes/indexes.kt
54+
:start-after: start-index-compound
55+
:end-before: end-index-compound
56+
:language: kotlin
57+
:copyable:
58+
:dedent:
59+
60+
The following is an example of a query that uses the index created in
61+
the preceding code sample:
62+
63+
.. io-code-block::
64+
:copyable: true
65+
66+
.. input:: /includes/indexes/indexes.kt
67+
:start-after: start-index-compound-query
68+
:end-before: end-index-compound-query
69+
:language: kotlin
70+
:dedent:
71+
72+
.. output::
73+
:visible: false
74+
75+
Movie(id=573a1392f29313caabcda755, title=China Seas, type=movie, genres=[Action, Drama, Adventure], ...)
76+
Movie(id=573a1392f29313caabcd9ca6, title=Scarface, type=movie, genres=[Action, Crime, Drama], ... )
77+
Movie(id=573a1392f29313caabcdb258, title=The Hurricane, type=movie, genres=[Action, Drama, Romance], ...)
78+
Movie(id=573a1391f29313caabcd820b, title=Beau Geste, type=movie, genres=[Action, Adventure, Drama], ...)
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+
To learn about effective indexing strategies using compound indexes, see
88+
:manual:`The ESR Rule </tutorial/equality-sort-range-rule>` in the {+mdb-server+} manual.
89+
90+
API Documentation
91+
~~~~~~~~~~~~~~~~~
92+
93+
To learn more about any of the methods discussed in this guide, see the following API
94+
documentation:
95+
96+
- `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__
97+
- `filter() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/filter.html>`__
98+
- `sort() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/sort.html>`__

source/indexes/single-field-index.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ in the {+mdb-server+} manual.
8888
API Documentation
8989
~~~~~~~~~~~~~~~~~
9090

91-
To learn more about any of the methods or types discussed in this
92-
guide, see the following API Documentation:
91+
To learn more about any of the methods discussed in this guide, see the following API
92+
documentation:
9393

9494
- `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__
9595
- `filter() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/filter.html>`__

source/work-with-indexes.txt

Lines changed: 2 additions & 1 deletion
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
--------
@@ -67,8 +68,8 @@ The following pages describe the most common index types and provide sample
6768
code for creating each index type.
6869

6970
- :ref:`kotlin-sync-single-field-index`
71+
- :ref:`kotlin-sync-compound-index`
7072

71-
.. TODO: - :ref:`kotlin-sync-compound-index`
7273
.. TODO: - :ref:`kotlin-sync-atlas-search-index`
7374

7475
Remove an Index

0 commit comments

Comments
 (0)