Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -7,7 +7,8 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
]

toc_landing_pages = [
"/read"
"/read",
"/indexes"
]

sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
Expand Down
87 changes: 87 additions & 0 deletions source/includes/usage-examples/index-code-examples.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.example
import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.client.model.ClusteredIndexOptions
import com.mongodb.client.model.Filters.*
import com.mongodb.client.model.IndexOptions
import com.mongodb.client.model.Indexes
import com.mongodb.client.*
import com.mongodb.kotlin.client.MongoClient
import org.bson.Document

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("<database name>")
val collection = database.getCollection<Document>("<collection name>")

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

// start-compound
collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"))
// end-compound

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

// start-search-create
val index = Document("mappings", Document("dynamic", true))
collection.createSearchIndex("<index name>", index)
// end-search-create

// start-search-list
val results = collection.listSearchIndexes()

results.forEach { result ->
println(result)
}
// end-search-list

// start-search-update
val newIndex = Document("mappings", Document("dynamic", true))
collection.updateSearchIndex("<index name>", newIndex)
// end-search-update

// start-search-delete
collection.dropIndex("<index name>")
// end-search-delete

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

// start-geo
collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"))
// end-geo

// start-unique
val indexOptions = IndexOptions().unique(true)
collection.createIndex(Indexes.ascending("<field name>"), indexOptions)
// end-unique

// start-wildcard
collection.createIndex(Indexes.ascending("$**"))
// end-wildcard

// start-clustered
val clusteredIndexOptions = ClusteredIndexOptions(
Indexes.ascending("_id"),
true
)

val collection = database.createCollection("<collection name>", clusteredIndexOptions)
// end-clustered

// start-remove
collection.dropIndex("<index name>")
// end-remove
}
3 changes: 2 additions & 1 deletion source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

.. toctree::

/read
/write-operations
/read
/indexes
/faq
/connection-troubleshooting
/issues-and-help
Expand Down
261 changes: 261 additions & 0 deletions source/indexes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
.. _kotlin-sync-indexes:

=================================
Optimize Queries by Using Indexes
=================================

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

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

.. meta::
: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

.. /work-with-indexes

Overview
--------

On this page, you can see copyable code examples that show how to manage different
types of indexes by using the {+driver-short+}.

.. TODO
.. .. tip::

.. To learn more about working with indexes, see the :ref:`kotlin-sync-work-with-indexes`
.. guide. To learn more about any of the indexes shown on this page, see the link
.. provided in each section.

To use an example from this page, copy the code example into the
:ref:`sample application <kotlin-sync-index-sample>` or your own application.
Be sure to replace all placeholders in the code examples, such as ``<connection string URI>``, with
the relevant values for your MongoDB deployment.

.. _kotlin-sync-index-sample:

.. include:: /includes/usage-examples/sample-app-intro.rst

.. literalinclude:: /includes/usage-examples/sample-app.kt
:language: kotlin
:copyable:
:linenos:
:emphasize-lines: 20-22

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

The following example creates an ascending index on the specified field:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-single-field
:end-before: end-single-field
:language: kotlin
:copyable:
:dedent:

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

Compound Index
--------------

The following example creates a compound index on the specified fields:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-compound
:end-before: end-compound
:language: kotlin
:copyable:
:dedent:

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

Multikey Index
--------------

The following example creates a multikey index on the specified array-valued field:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-multikey
:end-before: end-multikey
:language: kotlin
:copyable:
:dedent:

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

Geospatial Index
----------------

The following example creates a 2dsphere index on the specified field that contains
GeoJSON objects:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-geo
:end-before: end-geo
:language: kotlin
:copyable:
:dedent:

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

Unique Index
------------

The following example creates a unique index on the specified field:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-unique
:end-before: end-unique
:language: kotlin
:copyable:
:dedent:

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

Wildcard Index
--------------

The following example creates a wildcard index in the specified collection:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-wildcard
:end-before: end-wildcard
:language: kotlin
:copyable:
:dedent:

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

Clustered Index
---------------

The following example creates a new collection with a clustered index on the ``_id``
field:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-clustered
:end-before: end-clustered
:language: kotlin
:copyable:
:dedent:

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

Atlas Search Index Management
-----------------------------

The following sections contain code examples that describe how to manage Atlas Search
indexes.

.. TODO: To learn more about Atlas search indexes, see the :ref:`kotlin-sync-atlas-search-index`
.. guide.

Create Search Index
~~~~~~~~~~~~~~~~~~~

The following example creates an Atlas search index on the specified field:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-search-create
:end-before: end-search-create
:language: kotlin
:copyable:
:dedent:

.. TODO: To learn more about creating serach indexes, see the :ref:`kotlin-sync-atlas-search-index-create`
.. guide.

List Search Indexes
~~~~~~~~~~~~~~~~~~~

The following example prints a list of Atlas search indexes in the specified collection:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-search-list
:end-before: end-search-list
:language: kotlin
:copyable:
:dedent:

.. TODO: To learn more about listing search indexes, see the :ref:`kotlin-sync-atlas-search-index-list`
.. guide.

Update Search Indexes
~~~~~~~~~~~~~~~~~~~~~

The following example updates an existing Atlas search index with the specified
new index definition:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-search-update
:end-before: end-search-update
:language: kotlin
:copyable:
:dedent:

.. TODO: To learn more about updating search indexes, see the :ref:`kotlin-sync-atlas-search-index-update`
.. guide.

Delete Search Indexes
~~~~~~~~~~~~~~~~~~~~~

The following example deletes an Atlas search index with the specified name:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-search-delete
:end-before: end-search-delete
:language: kotlin
:copyable:
:dedent:

.. TODO: To learn more about deleting search indexes, see the :ref:`kotlin-sync-atlas-search-index-drop`
.. guide.

Text Index
----------

The following example creates a text index on the specified string field:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-text
:end-before: end-text
:language: kotlin
:copyable:
:dedent:

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

Delete an Index
---------------

The following example deletes an index with the specified name:

.. literalinclude:: /includes/usage-examples/index-code-examples.kt
:start-after: start-remove
:end-before: end-remove
:language: kotlin
:copyable:
:dedent:

.. TODO: To learn more about removing indexes, see :ref:`kotlin-sync-indexes-remove`
.. in the Work with Indexes guide.
Loading