diff --git a/source/includes/indexes/indexes.kt b/source/includes/indexes/indexes.kt index e1d776f8..23672969 100644 --- a/source/includes/indexes/indexes.kt +++ b/source/includes/indexes/indexes.kt @@ -12,7 +12,8 @@ data class Movie( @BsonId val id: ObjectId, val title: String? = "", - val genre: String? = "", + val type: String? = "", + val genres: List? = null, val cast: List? = null, val plot: String? = "", ) @@ -51,4 +52,21 @@ fun main() { println(result) } // end-index-single-query + + // start-index-compound + collection.createIndex(Indexes.ascending(Movie::type.name, Movie::genres.name)) + // end-index-compound + + // start-index-compound-query + val filter = and( + eq(Movie::type.name, "movie"), + `in`(Movie::genres.name, "Drama") + ) + val sort = Sorts.ascending(Movie::type.name, Movie::genres.name) + val results = collection.find(filter).sort(sort) + + results.forEach { result -> + println(result) + } + // end-index-compound-query } diff --git a/source/indexes/compound-index.txt b/source/indexes/compound-index.txt new file mode 100644 index 00000000..cf3942aa --- /dev/null +++ b/source/indexes/compound-index.txt @@ -0,0 +1,98 @@ +.. _kotlin-sync-compound-index: + +================ +Compound Indexes +================ + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: index, query, optimization, efficiency + +Overview +-------- + +:manual:`Compound indexes ` hold references to multiple +fields within a collection's documents, improving query and sort performance. + +When creating a compound index, you must specify the following: + +- The fields on which to create the index +- The sort order for each field (ascending or descending) + +Sample Data +~~~~~~~~~~~ + +The examples in this guide use the ``movies`` collection in the ``sample_mflix`` +database from the :atlas:`Atlas sample datasets `. To learn how to create a +free MongoDB Atlas cluster and load the sample datasets, see the +:atlas:`Get Started with Atlas ` guide. + +The following {+language+} data class models the documents in this collection: + +.. literalinclude:: /includes/indexes/indexes.kt + :start-after: start-movie-class + :end-before: end-movie-class + :language: kotlin + :copyable: + +Create a Compound Index +----------------------- + +The following example creates a compound index on the ``type`` and ``genre`` fields, with +both fields indexed in ascending order: + +.. literalinclude:: /includes/indexes/indexes.kt + :start-after: start-index-compound + :end-before: end-index-compound + :language: kotlin + :copyable: + :dedent: + +The following is an example of a query that uses the index created in +the preceding code sample: + +.. io-code-block:: + :copyable: true + + .. input:: /includes/indexes/indexes.kt + :start-after: start-index-compound-query + :end-before: end-index-compound-query + :language: kotlin + :dedent: + + .. output:: + :visible: false + + Movie(id=573a1392f29313caabcda755, title=China Seas, type=movie, genres=[Action, Drama, Adventure], ...) + Movie(id=573a1392f29313caabcd9ca6, title=Scarface, type=movie, genres=[Action, Crime, Drama], ... ) + Movie(id=573a1392f29313caabcdb258, title=The Hurricane, type=movie, genres=[Action, Drama, Romance], ...) + Movie(id=573a1391f29313caabcd820b, title=Beau Geste, type=movie, genres=[Action, Adventure, Drama], ...) + ... + +Additional Information +---------------------- + +To learn more about compound indexes, see :manual:`Compound Indexes ` +in the {+mdb-server+} manual. + +To learn about effective indexing strategies using compound indexes, see +:manual:`The ESR Rule ` in the {+mdb-server+} manual. + +API Documentation +~~~~~~~~~~~~~~~~~ + +To learn more about any of the methods discussed in this guide, see the following API +documentation: + +- `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__ +- `filter() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/filter.html>`__ +- `sort() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/sort.html>`__ \ No newline at end of file diff --git a/source/indexes/single-field-index.txt b/source/indexes/single-field-index.txt index cbbe894d..bcfd719b 100644 --- a/source/indexes/single-field-index.txt +++ b/source/indexes/single-field-index.txt @@ -88,8 +88,8 @@ in the {+mdb-server+} manual. API Documentation ~~~~~~~~~~~~~~~~~ -To learn more about any of the methods or types discussed in this -guide, see the following API Documentation: +To learn more about any of the methods discussed in this guide, see the following API +documentation: - `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__ - `filter() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/filter.html>`__ diff --git a/source/work-with-indexes.txt b/source/work-with-indexes.txt index 053b7704..fbd0b417 100644 --- a/source/work-with-indexes.txt +++ b/source/work-with-indexes.txt @@ -20,6 +20,7 @@ Work with Indexes .. toctree:: /indexes/single-field-index.txt + /indexes/compound-index.txt Overview -------- @@ -67,8 +68,8 @@ The following pages describe the most common index types and provide sample code for creating each index type. - :ref:`kotlin-sync-single-field-index` +- :ref:`kotlin-sync-compound-index` -.. TODO: - :ref:`kotlin-sync-compound-index` .. TODO: - :ref:`kotlin-sync-atlas-search-index` Remove an Index