-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-41143: Compound Indexes #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [s] Would linking to this resource be helpful for the reader: https://www.mongodb.com/docs/manual/tutorial/equality-sort-range-rule/#std-label-esr-indexing-rule |
||
|
||
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>`__ |
There was a problem hiding this comment.
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 aFilters
method. To use the method as such you need to surround it with backticks.