diff --git a/snooty.toml b/snooty.toml index a9043f1f..77be8b46 100644 --- a/snooty.toml +++ b/snooty.toml @@ -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/" diff --git a/source/includes/usage-examples/index-code-examples.kt b/source/includes/usage-examples/index-code-examples.kt new file mode 100644 index 00000000..b97f2c0e --- /dev/null +++ b/source/includes/usage-examples/index-code-examples.kt @@ -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 = "" + + val settings = MongoClientSettings.builder() + .applyConnectionString(ConnectionString(uri)) + .retryWrites(true) + .build() + + val mongoClient = MongoClient.create(settings) + val database = mongoClient.getDatabase("") + val collection = database.getCollection("") + + // start-single-field + collection.createIndex(Indexes.ascending("", "")) + // end-compound + + // start-multikey + collection.createIndex(Indexes.ascending("")) + // end-multikey + + // start-search-create + val index = Document("mappings", Document("dynamic", true)) + collection.createSearchIndex("", 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("", newIndex) + // end-search-update + + // start-search-delete + collection.dropIndex("") + // end-search-delete + + // start-text + collection.createIndex(Indexes.text("")) + // end-text + + // start-geo + collection.createIndex(Indexes.geo2dsphere("")) + // end-geo + + // start-unique + val indexOptions = IndexOptions().unique(true) + collection.createIndex(Indexes.ascending(""), indexOptions) + // end-unique + + // start-wildcard + collection.createIndex(Indexes.ascending("$**")) + // end-wildcard + + // start-clustered + val clusteredIndexOptions = ClusteredIndexOptions( + Indexes.ascending("_id"), + true + ) + + val collection = database.createCollection("", clusteredIndexOptions) + // end-clustered + + // start-remove + collection.dropIndex("") + // end-remove +} diff --git a/source/index.txt b/source/index.txt index a609afdf..2cfa83a4 100644 --- a/source/index.txt +++ b/source/index.txt @@ -13,8 +13,9 @@ .. toctree:: - /read /write-operations + /read + /indexes /faq /connection-troubleshooting /issues-and-help diff --git a/source/indexes.txt b/source/indexes.txt new file mode 100644 index 00000000..94e6674f --- /dev/null +++ b/source/indexes.txt @@ -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 ` or your own application. +Be sure to replace all placeholders in the code examples, such as ````, 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. \ No newline at end of file