Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion source/includes/indexes/indexes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ data class Movie(
@BsonId
val id: ObjectId,
val title: String? = "",
val genre: String? = "",
val type: String? = "",
val genres: List<String>? = null,
val cast: List<String>? = null,
val plot: String? = "",
)
Expand Down Expand Up @@ -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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Kotlin (and probably Java) quirk – in is a language keyword AND a Filters method. To use the method as such you need to surround it with backticks.

)
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
}
95 changes: 95 additions & 0 deletions source/indexes/compound-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
.. _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 </core/index-compound/>` 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] Is there a default sort order if the sort order isn't specified or is specifying always required?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's required – source


Sample Data
~~~~~~~~~~~

The examples in this guide use the ``movies`` collection in the ``sample_mflix``
database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see the
:atlas:`Get Started with Atlas </getting-started>` 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 example:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[s] Maybe this introduction can be reworded to improve clarity? It can be hard to parse "following example" vs "preceding example" all in one sentence.


.. 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 </core/index-compound>`
in the {+mdb-server+} manual.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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>`__
4 changes: 2 additions & 2 deletions source/indexes/single-field-index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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>`__
Expand Down
3 changes: 2 additions & 1 deletion source/work-with-indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Work with Indexes
.. toctree::

/indexes/single-field-index.txt
/indexes/compound-index.txt

Overview
--------
Expand Down Expand Up @@ -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
Expand Down
Loading