Overview
重要
このページで説明されている機能は、 Kotlinドライバー バージョン 5.3 以降でのみ使用できます。
このガイドでは、 Kotlinドライバーで使用可能なビルダ クラスを使用してデータクラスのプロパティを直接使用する方法を学習できます。
Kotlinドライバーは、stringフィールド名を使用する代わりにビルダ メソッドを使用するときにデータクラスプロパティを参照拡張機能を実装します。 この方法でコードを構造化すると、コードの型安全性を高め、アプリケーションKotlin の相互運用性を向上させます。
拡張機能ライブラリでは、インデックス表記を使用してクエリを構築したり、ドキュメントを更新したり、その他のステートメントを記述したりすることもできます。この表記の詳細については、 Kotlin参照ドキュメントの「 Infix 表記 」を参照してください。
注意
このページでは、この機能を示す限定数のコード例を紹介します。 すべてのビルダ クラスの例については、 ビルダ ガイドを参照してください。
Kotlin拡張機能をプロジェクトに追加する
この機能を実装するには、mongodb-driver-kotlin-extensions 依存関係を依存関係リストに追加する必要があります。
Gradle と Mavenパッケージマネージャーを使用してプロジェクトに拡張依存関係を追加する方法を確認するには、次のタブから を選択します。
Gradle を使用して依存関係を管理している場合は、次のものをbuild.gradle.kts 依存関係リストに追加します。
build. Gradle.kts
implementation("org.mongodb:mongodb-driver-kotlin-extensions")Maven を使用して依存関係を管理している場合は、次のものをpom.xml 依存関係リストに追加します。
pom.xml
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-kotlin-extensions</artifactId></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.eqimport com.mongodb.kotlin.client.model.Filters.allval student = Student( "Sandra Nook", listOf("Alvarez", "Gruber"), 85.7)
// Equivalent equality queriesStudent::name.eq(student.name)eq(Student::name, student.name)Student::name eq student.name // Infix notation
// Equivalent array queriesall(Student::teachers, student.teachers)Student::teachers.all(student.teachers)Student::teachers all student.teachers // Infix notationIndexes
Indexes ビルダークラスのヘルパーを使用して、データクラスプロパティにインデックスを作成できます。 このクラスの詳細については、 インデックス ビルダのガイドを参照してください。
次のコードは、Indexes 拡張メソッドを使用して Student データクラスのインデックスを作成するさまざまな方法を示しています。
import com.mongodb.kotlin.client.model.Indexes.ascendingimport com.mongodb.kotlin.client.model.Indexes.descendingval 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.excludeIdimport com.mongodb.kotlin.client.model.Projections.fieldsimport com.mongodb.kotlin.client.model.Projections.includeval combinedProj = fields( include(Student::name, Student::gradeAverage), excludeId())
collection.find().projection(combinedProj)ソート
Sorts ビルダークラスのヘルパーを使用して、データクラスプロパティでソートできます。 このクラスの詳細については、 ソート ビルダのガイドを参照してください。
次のコードは、Sorts 拡張メソッドを使用して Student データクラスでさまざまな並べ替えを作成する方法を示しています。
import com.mongodb.client.model.Sorts.orderByimport com.mongodb.kotlin.client.model.Sortsval 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.gteimport com.mongodb.kotlin.client.model.Updates.addToSetimport com.mongodb.kotlin.client.model.Updates.combineimport com.mongodb.kotlin.client.model.Updates.maxval filter = Student::gradeAverage gte 85.0val 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.groupimport com.mongodb.client.model.Aggregates.limitimport com.mongodb.client.model.Aggregates.sortimport com.mongodb.kotlin.client.model.Accumulators.avg// Data class to store aggregation resultdata 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)