将构建器与数据类结合使用
Overview
在本指南中,您可以学习;了解如何将数据类属性直接与Kotlin驾驶员中提供的构建器类一起使用。
Kotlin驾驶员实现了一些扩展,允许您在使用构建器方法(而不是使用字符串字段名称)时引用数据类属性。您可以通过这种方式构建代码,从而提高代码的类型安全性,并提高应用程序的Kotlin互操作性。
扩展库还允许使用中缀表示法构造查询、更新文档和写入其他语句。要学习;了解有关此表示法的详情,请参阅Kotlin参考文档中的 中缀表示法 。
为项目添加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
路径导入类和方法来使用扩展方法。您可以在同一应用程序中混合使用这些方法和标准构建器方法,如本指南中的聚合示例所示。
构建器示例
本节包含的示例演示如何将数据类属性直接与扩展包中的构建器类方法一起使用。
提示
数据类注解
样本数据
本部分中的示例使用 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
构建者类中的助手,通过数据类属性执行更新。要学习;了解有关此类的更多信息,请参阅 Updates Builders指南。
以下代码展示了如何使用 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)