Docs 菜单
Docs 主页
/ / /
Kotlin 协程
/ /

将构建器与数据类结合使用

在此页面上

  • Overview
  • 为项目添加Kotlin扩展
  • 构建器示例
  • 样本数据
  • 筛选器
  • 索引
  • 投影
  • 排序
  • 更新
  • 聚合
  • API 文档

在本指南中,您可以学习;了解如何将数据类属性直接与Kotlin驾驶员中提供的构建器类一起使用。

Kotlin驾驶员实现了一些扩展,允许您在使用构建器方法(而不是使用字符串字段名称)时引用数据类属性。您可以通过这种方式构建代码,从而提高代码的类型安全性,并提高应用程序的Kotlin互操作性。

扩展库还允许使用中缀表示法构造查询、更新文档和写入其他语句。要学习;了解有关此表示法的详情,请参阅Kotlin参考文档中的 中缀表示法 。

注意

本页提供了有限数量的代码示例来演示此功能。要查看所有构建器类的示例,请参阅 构建器指南。

要实现此功能,必须将 mongodb-driver-kotlin-extensions 依赖项添加到依赖项列表中。

从以下标签页中进行选择,查看如何使用 GradleMaven包管理器将扩展依赖项添加到项目中:

如果您使用 Gradle 要管理依赖项,请将以下内容添加到build.gradle.kts 依赖项列表中:

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

如果您使用的是 Maven 要管理依赖项,请将以下内容添加到pom.xml 依赖项列表中:

pom.xml
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-kotlin-extensions</artifactId>
<version>5.3.0</version>
</dependency>

安装扩展依赖项后,可以通过从com.mongodb.kotlin.client.model 路径导入类和方法来使用扩展方法。您可以在同一应用程序中混合使用这些方法和标准构建器方法,如本指南中的聚合示例所示。

本节包含的示例演示如何将数据类属性直接与扩展包中的构建器类方法一起使用。

提示

数据类注解

当您使用数据类的扩展构建器类方法时,这些方法会遵循bson-kotlinbson-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 构建者类中的助手,通过数据类属性执行更新。要学习;了解有关此类的更多信息,请参阅 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)

您可以使用AggregatesAccumulators 构建者类中的助手,通过数据类属性来执行聚合。要学习;了解有关这些类的更多信息,请参阅 聚合构建器指南。

以下代码展示了如何使用 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)
  • Kotlin驾驶员扩展

后退

Update