MongoDB.local SF, Jan 15: See the speaker lineup & ship your AI vision faster. Use WEB50 to save 50%
Find out more >
Docs Menu
Docs Home
/ /
Kotlin Sync ドライバー

インデックスを使用したクエリの最適化

このページでは、 Kotlin Sync ドライバーを使用してさまざまなタイプのインデックスを管理する方法を示すコピー可能なコード例を紹介します。

このページの例を使用するには、コード例をサンプルアプリケーションまたは独自のアプリケーションにコピーします。<connection string URI> などのコード例内のすべてのプレースホルダーを、 MongoDBデプロイに関連する値に置き換えてください。

次のサンプルアプリケーションを使用して、このページのコード例をテストできます。 サンプル アプリケーションを使用するには、次の手順を実行します。

  1. Maven または Gradle プロジェクトに Kotlin Sync ドライバーがインストールされていることを確認します。

  2. 次のコードをコピーし、新しい.ktファイルに貼り付けます。

  3. このページからコード例をコピーし、 ファイル内の指定された行に貼り付けます。

1import com.mongodb.ConnectionString
2import com.mongodb.MongoClientSettings
3import com.mongodb.kotlin.client.*
4import com.mongodb.client.model.Filters.*
5import org.bson.Document
6
7fun main() {
8 val uri = "<connection string URI>"
9
10 val settings = MongoClientSettings.builder()
11 .applyConnectionString(ConnectionString(uri))
12 .retryWrites(true)
13 .build()
14
15 // Create a new client and connect to the server
16 val mongoClient = MongoClient.create(settings)
17 val database = mongoClient.getDatabase("<database name>")
18 val collection = database.getCollection<Document>("<collection name>")
19
20 // Start example code here
21
22 // End example code here
23}

次の例では、指定されたフィールドに昇順のインデックスを作成しています。

collection.createIndex(Indexes.ascending("<field name>"))

単一フィールドインデックスの詳細については、単一フィールド インデックスガイドをご覧ください。

次の例では、指定されたフィールドに複合インデックスを作成しています。

collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"))

次の例では、指定された配列値フィールドにマルチキー インデックスを作成します。

collection.createIndex(Indexes.ascending("<array field name>"))

次の例では、GeoJSON オブジェクトを含む指定されたフィールドに2 dsphere インデックスを作成します。

collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"))

次の例では、指定されたフィールドに一意のインデックスを作成しています。

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

次の例では、 指定されたコレクションにワイルドカード インデックスを作成します。

collection.createIndex(Indexes.ascending("$**"))

次の例では、 _idフィールドにクラスター化インデックスを持つ新しいコレクションを作成します。

val clusteredIndexOptions = ClusteredIndexOptions(
Indexes.ascending("_id"),
true
)
val collection = database.createCollection("<collection name>", clusteredIndexOptions)

次のセクションには、Atlas Search インデックスを管理する方法を説明するコード例が含まれています。

次の例では、指定されたフィールドに Atlas Searchインデックスを作成します。

val index = Document("mappings", Document("dynamic", true))
collection.createSearchIndex("<index name>", index)

次の例では、指定されたコレクション内の Atlas Search インデックスの一覧を出力します。

val results = collection.listSearchIndexes()
results.forEach { result ->
println(result)
}

次の例では、指定された新しいインデックス定義で既存の Atlas Searchインデックスをアップデートします。

val newIndex = Document("mappings", Document("dynamic", true))
collection.updateSearchIndex("<index name>", newIndex)

次の例では、指定された名前の Atlas Searchインデックスを削除します。

collection.dropIndex("<index name>")

次の例では、指定された string フィールドにテキスト インデックスを作成します。

collection.createIndex(Indexes.text("<field name>"))

次の例では、指定された名前のインデックスを 1 つ削除します。

collection.dropIndex("<index name>")

戻る

個別のフィールド値の取得