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
3 changes: 2 additions & 1 deletion snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",

toc_landing_pages = [
"/read",
"/indexes"
"/indexes",
"work-with-indexes",
]

sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
Expand Down
54 changes: 54 additions & 0 deletions source/includes/indexes/indexes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.client.model.Indexes
import com.mongodb.client.model.Filters
import com.mongodb.client.model.Sorts
import com.mongodb.kotlin.client.MongoClient
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.types.ObjectId

// start-movie-class
data class Movie(
@BsonId
val id: ObjectId,
val title: String? = "",
val genre: String? = "",
val cast: List<String>? = null,
val plot: String? = "",
)
// end-movie-class

fun main() {
val uri = "<connection string URI>"

val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString(uri))
.retryWrites(true)
.build()

val mongoClient = MongoClient.create(settings)
val database = mongoClient.getDatabase("sample_mflix")
val collection = database.getCollection<Movie>("movies")

// start-remove-index
collection.dropIndex("_title_")
// end-remove-index

// start-remove-all-indexes
collection.dropIndexes()
// end-remove-all-indexes

// start-index-single
collection.createIndex(Indexes.ascending(Movie::title.name))
// end-index-single

// start-index-single-query
val filter = Filters.eq(Movie::title.name, "Batman")
val sort = Sorts.ascending(Movie::title.name)
val results = collection.find(filter).sort(sort)

results.forEach { result ->
println(result)
}
// end-index-single-query
}
2 changes: 1 addition & 1 deletion source/includes/usage-examples/index-code-examples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun main() {
val collection = database.getCollection<Document>("<collection name>")

// start-single-field
collection.createIndex(Indexes.ascending("<field name"))
collection.createIndex(Indexes.ascending("<field name>"))
// end-single-field

// start-compound
Expand Down
12 changes: 5 additions & 7 deletions source/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ Optimize Queries by Using Indexes
:description: Learn how to use indexes by using the MongoDB Kotlin Sync driver.
:keywords: query, optimization, efficiency, usage example, code example

.. TODO
.. .. toctree::
.. :titlesonly:
.. :maxdepth: 1
.. toctree::
:titlesonly:
:maxdepth: 1

.. /work-with-indexes
/work-with-indexes

Overview
--------
Expand Down Expand Up @@ -65,8 +64,7 @@ The following example creates an ascending index on the specified field:
:copyable:
:dedent:

.. TODO: To learn more about single field indexes, see the
.. :ref:`kotlin-sync-single-field-index` guide.
To learn more about single field indexes, see the :ref:`kotlin-sync-single-field-index` guide.

Compound Index
--------------
Expand Down
77 changes: 77 additions & 0 deletions source/indexes/single-field-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.. _kotlin-sync-single-field-index:

====================
Single-Field 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:`Single-field indexes </core/index-single/>` are indexes with a reference to a
single field within a collection's documents. They improve single field query and sort
performance, and support :manual:`TTL Indexes </core/index-ttl>` that automatically remove
documents from a collection after a certain amount of time or at a specific clock time.

.. note::

The ``_id_`` index is an example of a single-field index. This index is automatically
created on the ``_id`` field when a new collection is created.

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 documents in this collection are modeled by the following {+language+} data class:

.. literalinclude:: /includes/indexes/indexes.kt
:start-after: start-movie-class
:end-before: end-movie-class
:language: kotlin
:copyable:

Create Single-Field Index
-------------------------

The following example creates an index in ascending order on the ``title`` field:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q:Are there options to create an index in any other order other than ascending and descending?

S: Clarify/state explicitly that you can create an index in either ascending or descending order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will add to page overview.


.. literalinclude:: /includes/indexes/indexes.kt
:start-after: start-index-single
:end-before: end-index-single
:language: kotlin
:copyable:
:dedent:

The following is an example of a query that is covered by the index created in the preceding code example:

.. io-code-block::
:copyable: true

.. input:: /includes/indexes/indexes.kt
:start-after: start-index-single-query
:end-before: end-index-single-query
:language: kotlin
:dedent:

.. output::
:visible: false

Movie(id=573a1398f29313caabceb515, title=Batman, ...)

To learn more, see :manual:`Single Field Indexes </core/index-single>` in the {+mdb-server+} manual.
126 changes: 126 additions & 0 deletions source/work-with-indexes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
.. _kotlin-sync-work-with-indexes:

=================
Work with Indexes
=================

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: query, optimization, efficiency

.. toctree::

/indexes/single-field-index.txt

Overview
--------

In this guide, you can learn how to use **indexes** with the {+driver-short+}.
Indexes can improve the efficiency of queries and add functionality to querying and
storing documents.

Without indexes, MongoDB must scan every document in a collection to find the
documents that match each query. These collection scans are slow and can negatively affect
the performance of your application. However, if an appropriate index exists for a query,
MongoDB can use the index to limit the documents it must inspect.

Operational Considerations
~~~~~~~~~~~~~~~~~~~~~~~~~~

To improve query performance, build indexes on fields that appear often in
your application's queries and operations that return sorted results. Each
index that you add consumes disk space and memory when active, so we recommend
that you track index memory and disk usage for capacity planning. In addition,
when a write operation updates an indexed field, MongoDB updates the related
Copy link
Collaborator

Choose a reason for hiding this comment

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

S: I see that in the server manual, it talks about how indexes are not always the best when using write operations. I believe that the sentence starting with "In addition, when a write.." is implying that indices can negatively affect write operation performance, but it would be great to make that clearer.

index.

Because MongoDB supports dynamic schemas, applications can query against fields
whose names are not known in advance or are arbitrary. MongoDB 4.2 introduced
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 it it necessary to say what version of MongoDB introduced wildcard indexes, or can we just say that wildcard indexes support queries with fields that have arbitrary/unknown names?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can reword.

:manual:`wildcard indexes </core/index-wildcard/>` to help support these
queries. Wildcard indexes are not designed to replace workload-based index
planning.

For more information about designing your data model and choosing indexes appropriate for your application, see the
:manual:`Data Modeling and Indexes </core/data-model-operations/#indexes>` guide
in the {+mdb-server+} manual.

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.

Create an Index
---------------

MongoDB supports several different index types to support querying your data.
The following pages describe the most common index types and provide sample
code for creating each index type.

- :ref:`kotlin-sync-single-field-index`

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

Remove an Index
---------------

You can remove any unused index except the default unique index on the
``_id`` field.

The following sections show how to remove a single index or to remove all
indexes in a collection.

Delete a Single Index
~~~~~~~~~~~~~~~~~~~~~

Pass an index name to the ``dropIndex()`` method to remove an index from a collection.

The following example removes an index with the name ``"_title_"`` from the ``movies``
collection:

.. literalinclude:: /includes/indexes/indexes.kt
:language: kotlin
:start-after: start-remove-index
:end-before: end-remove-index
:dedent:

.. note::

You cannot remove a single field from a compound text index. You must
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q: I don't know what a compound text index is; is that information you expect the user to have already going into this page?

Copy link
Collaborator Author

@mcmorisi mcmorisi Jul 19, 2024

Choose a reason for hiding this comment

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

All types of indexes are slated to receive their own page – this page will link to the compound text index page once it's written.

drop the entire index and create a new one to update the indexed
fields.

Delete All Indexes
~~~~~~~~~~~~~~~~~~

Starting with MongoDB 4.2, you can drop all indexes by calling the
``dropIndexes()`` method on your collection:

.. literalinclude:: /includes/indexes/indexes.kt
:language: kotlin
:start-after: start-remove-all-indexes
:end-before: end-remove-all-indexes
:dedent:

API Documentation
~~~~~~~~~~~~~~~~~

To learn more about any of the methods or types discussed in this
guide, see the following API documentation:

- `createIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-index.html>`__
- `createIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-indexes.html>`__
- `dropIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/drop-index.html>`__
- `dropIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/drop-indexes.html>`__
Loading