-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-41141: Indexes #13
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 4 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,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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
.. toctree:: | ||
|
||
/read | ||
/indexes | ||
/faq | ||
/connection-troubleshooting | ||
/issues-and-help | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,258 @@ | ||||||
.. _kotlin-sync-indexes: | ||||||
|
||||||
============================= | ||||||
Optimize Queries with Indexes | ||||||
============================= | ||||||
|
||||||
.. contents:: On this page | ||||||
:local: | ||||||
:backlinks: none | ||||||
:depth: 2 | ||||||
:class: singlecol | ||||||
|
||||||
.. facet:: | ||||||
:name: genre | ||||||
:values: reference | ||||||
|
||||||
.. meta:: | ||||||
:description: Learn how to use indexes with the MongoDB {+driver-short+} driver. | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
: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 work with common | ||||||
types of indexes that you can use with the {+driver-short+}. | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
.. 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 field that contains | ||||||
array values: | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
.. 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. | ||||||
|
||||||
Atlas Search Index | ||||||
------------------ | ||||||
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: This section could be called Atlas Search Index Management 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: what is the rationale to have this section in this location in the TOC? 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. The honest answer is that I just grabbed the format from Pymongo's equivalent. I reached out to Jordan (who wrote the Pymongo page) and he mentioned his placement was based on the Java page. Do you have strong feelings about shifting the section up or down? 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. I dont have strong feelings, but i would suggest moving the section to the end of the list of index types as this is a separate index category with different methods |
||||||
|
||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
.. 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: | ||||||
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: describe the update
Suggested change
|
||||||
|
||||||
.. 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 field: | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
.. 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. | ||||||
|
||||||
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. | ||||||
|
||||||
Remove an Index | ||||||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
--------------- | ||||||
|
||||||
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. |
Uh oh!
There was an error while loading. Please reload this page.