AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

データ クラスを持つビルダの使用

重要

このページで説明されている機能は、 Kotlinドライバー バージョン 5.3 以降でのみ使用できます。

このガイドでは、 Kotlinドライバーで使用可能なビルダ クラスを使用してデータクラスのプロパティを直接使用する方法を学習できます。

Kotlinドライバーは、stringフィールド名を使用する代わりにビルダ メソッドを使用するときにデータクラスプロパティを参照拡張機能を実装します。 この方法でコードを構造化すると、コードの型安全性を高め、アプリケーションKotlin の相互運用性を向上させます。

拡張機能ライブラリでは、インデックス表記を使用してクエリを構築したり、ドキュメントを更新したり、その他のステートメントを記述したりすることもできます。この表記の詳細については、 Kotlin参照ドキュメントの「 Infix 表記 」を参照してください。

注意

このページでは、この機能を示す限定数のコード例を紹介します。 すべてのビルダ クラスの例については、 ビルダ ガイドを参照してください。

この機能を実装するには、mongodb-driver-kotlin-extensions 依存関係を依存関係リストに追加する必要があります。

Tip

ドキュメントの請求書

ドライバー アーティファクトのバージョンを管理するには、アプリケーションにJVMドライバーの請求書(BOM) をアプリケーションに追加することをお勧めします。これにより、BOM でカバーされる個々のパッケージのバージョンを指定する必要がなくなり、依存関係の管理が簡素化されます。詳しくは、 クイック スタートガイドの「 ドライバー請求の追加 」ステップを参照してください。

Gradle と Mavenパッケージマネージャーを使用してプロジェクトに拡張依存関係を追加する方法を確認するには、次のタブから を選択します。

Gradle を使用して依存関係を管理している場合は、次のものをbuild.gradle.kts 依存関係リストに追加します。

build. Gradle.kts
implementation("org.mongodb:mongodb-driver-kotlin-extensions")

After you install the extensions dependency, you can use the extension methods by importing classes and methods from the com.mongodb.kotlin.client.model path. You can mix usage of these methods and the standard builder methods in the same application, as shown in the Aggregates example in this guide.

このセクションには、 拡張機能パッケージのビルダクラスメソッドを使用してデータクラスプロパティを直接使用する方法を示す例えが含まれています。

Tip

データクラスの注釈

拡張ビルダクラスがデータ クラスをメソッドを使用する場合、メソッドは bson-kotlin パッケージと bson-kotlinx パッケージのデータクラスの注釈を尊重します。注釈の詳細については、 ドキュメント データ形式: データ クラスガイドの 「注釈を使用したコンポーネント変換の指定」 セクション、およびKotlin直列化ガイドの 「データ クラスの注釈」 セクションを参照してください。

このセクションの例では、学校の学生を説明する studentsコレクション内のドキュメントを使用します。studentsコレクション内のドキュメントは、次のKotlinデータクラスによってモデル化されます。

data class Student(
val name: String,
val teachers: List<String>,
val gradeAverage: Double
)

Filters ビルダークラスのヘルパーを使用して、データクラスプロパティをクエリできます。このクラスの詳細については、フィルター ビルダのガイドを参照してください。

次のコードは、Filters 拡張メソッドを使用して Student データクラスでクエリを実行するさまざまな方法を示しています。

import com.mongodb.kotlin.client.model.Filters.eq
import com.mongodb.kotlin.client.model.Filters.all
val student = Student(
"Sandra Nook",
listOf("Alvarez", "Gruber"),
85.7
)
// Equivalent equality queries
Student::name.eq(student.name)
eq(Student::name, student.name)
Student::name eq student.name // Infix notation
// Equivalent array queries
all(Student::teachers, student.teachers)
Student::teachers.all(student.teachers)
Student::teachers all student.teachers // Infix notation

Indexes ビルダークラスのヘルパーを使用して、データクラスプロパティにインデックスを作成できます。 このクラスの詳細については、 インデックス ビルダのガイドを参照してください。

次のコードは、Indexes 拡張メソッドを使用して Student データクラスのインデックスを作成するさまざまな方法を示しています。

import com.mongodb.kotlin.client.model.Indexes.ascending
import com.mongodb.kotlin.client.model.Indexes.descending
val ascendingIdx = Indexes.ascending(Student::name)
val descendingIdx = Indexes.descending(Student::teachers)
val ascIdxName = collection.createIndex(ascendingIdx)
val descIdxName = collection.createIndex(descendingIdx)

Projections ビルダークラスのヘルパーを使用して、データクラスプロパティのプロジェクションを作成できます。このクラスの詳細については、「プロジェクション ビルダガイド」を参照してください。

次のコードは、Projections 拡張メソッドを使用して Student データクラスにプロジェクションを作成する方法を示しています。

import com.mongodb.kotlin.client.model.Projections.excludeId
import com.mongodb.kotlin.client.model.Projections.fields
import com.mongodb.kotlin.client.model.Projections.include
val combinedProj = fields(
include(Student::name, Student::gradeAverage),
excludeId()
)
collection.find().projection(combinedProj)

Sorts ビルダークラスのヘルパーを使用して、データクラスプロパティでソートできます。 このクラスの詳細については、 ソート ビルダのガイドを参照してください。

次のコードは、Sorts 拡張メソッドを使用して Student データクラスでさまざまな並べ替えを作成する方法を示しています。

import com.mongodb.client.model.Sorts.orderBy
import com.mongodb.kotlin.client.model.Sorts
val sort = orderBy(
Sorts.descending(Student::gradeAverage),
Sorts.ascending(Student::name)
)
collection.find().sort(sort)

Updates ビルダークラスのヘルパーを使用して、データクラスのプロパティを使用してアップデートを実行できます。このクラスの詳細については、アップデート ビルダのガイドを参照してください。

次のコードは、Sorts 拡張メソッドを使用して Student データクラスでさまざまな並べ替えを作成する方法を示しています。

import com.mongodb.kotlin.client.model.Filters.gte
import com.mongodb.kotlin.client.model.Updates.addToSet
import com.mongodb.kotlin.client.model.Updates.combine
import com.mongodb.kotlin.client.model.Updates.max
val filter = Student::gradeAverage gte 85.0
val update = combine(
addToSet(Student::teachers, "Soto"),
Student::gradeAverage.max(90.0)
)
collection.updateMany(filter, update)

Aggregates と Accumulators ビルダー クラスのヘルパーを使用して、データクラスのプロパティを使用して集計を実行できます。これらのクラスの詳細については、集計ビルダのガイドを参照してください。

次のコードは、Accumulators 拡張メソッドと Aggregatesヘルパーメソッドを使用して Student データクラスで集計を実行する方法を示しています。

import com.mongodb.client.model.Aggregates.group
import com.mongodb.client.model.Aggregates.limit
import com.mongodb.client.model.Aggregates.sort
import com.mongodb.kotlin.client.model.Accumulators.avg
// Data class to store aggregation result
data class Summary ( val average: Double )
val pipeline = listOf(
// Sorts grades from high to low
sort(Sorts.descending(Student::gradeAverage)),
// Selects the top 3 students
limit(3),
// Calculates the average of their grades and stores value in a Summary instance
group(null, avg(Summary::average, "\$${Student::gradeAverage.name}"))
)
val result = collection.aggregate<Summary>(pipeline)