データ クラスを持つビルダの使用
Overview
このガイドでは、 Kotlinドライバーで使用可能なビルダ クラスを使用してデータクラスのプロパティを直接使用する方法を学習できます。
Kotlinドライバーは、stringフィールド名を使用する代わりにビルダ メソッドを使用するときにデータクラスプロパティを参照拡張機能を実装します。この方法でコードを構造化すると、コードの型安全性を高め、アプリケーションKotlin の相互運用性を向上させます。
拡張機能ライブラリでは、インデックス表記を使用してクエリを構築したり、ドキュメントを更新したり、その他のステートメントを記述したりすることもできます。この表記の詳細については、 Kotlin参照ドキュメントの「 Infix 表記 を参照してください。
注意
このページでは、この機能を示す限定数のコード例を紹介します。すべてのビルダ クラスの例については、「 ビルダ ガイド 」を参照してください。
Kotlin拡張機能をプロジェクトに追加する
この機能を実装するには、mongodb-driver-kotlin-extensions
依存関係を依存関係リストに追加する必要があります。
Gradle と Mavenパッケージマネージャーを使用してプロジェクトに拡張依存関係を追加する方法を確認するには、次のタブから を選択します。
Gradle を使用している場合 依存関係を管理するには、次のものをbuild.gradle.kts
依存関係リストに追加します。
implementation("org.mongodb:mongodb-driver-kotlin-extensions:5.3.0")
Maven を使用している場合 依存関係を管理するには、次のものをpom.xml
依存関係リストに追加します。
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-kotlin-extensions</artifactId> <version>5.3.0</version> </dependency>
拡張機能の依存関係をインストールしたら、com.mongodb.kotlin.client.model
パスからクラスとメソッドをインポートして、拡張メソッドを使用できるようになります。このガイドの 集計の例に示されているように、同じアプリケーション内でこれらのメソッドと標準ビルダ メソッドを混在させることができます。
ビルダの例
このセクションには、 拡張機能パッケージのビルダクラスメソッドを使用してデータクラスプロパティを直接使用する方法を示す例えが含まれています。
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
ビルダークラスのヘルパーを使用して、データクラスプロパティにインデックスを作成できます。このクラスの詳細については、 インデックス ビルダのガイドを参照してください。
次のコードは、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
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)