Overview
在本指南中,您可以学习;了解如何使用Kotlin数据类在MongoDB Kotlin驱动程序中存储和检索数据。
对数据类进行序列化和反序列化
该驱动程序本身支持使用默认编解码器注册表对 MongoDB 读写操作的 Kotlin 数据类型进行编码和解码。默认编解码器注册表是称为编解码器的类的集合,定义如何编码和解码 Kotlin 和 Java 类型。
数据类示例
本节中的代码样本引用以下样本数据类,其中描述了数据存储设备:
data class DataStorage(val productName: String, val capacity: Double)插入数据类
您可以插入 DataStorage 实例,如以下代码所示:
val collection = database.getCollection<DataStorage>("data_storage")val record = DataStorage("tape", 5.0)collection.insertOne(record)检索数据类
如以下代码所示,您可以按 DataStorage 实例检索文档并打印:
val collection = database.getCollection<DataStorage>("data_storage_devices")
// Retrieve and print the documents as data classesval resultsFlow = collection.find()resultsFlow.collect { println(it) }DataStorage(productName=tape, capacity=5.0)提示
构建器方法和数据类属性
通过将可选的Kotlin驱动程序扩展依赖项添加到应用程序,您可以直接将构建者类中的方法与数据类属性一起使用。要学习;了解更多信息和查看示例,请参阅《使用构建器与数据类》指南。
您可以为从集合返回的文档指定一个类,即使该类与您在检索集合时指定的类不同。
以下示例对上一示例中由 DataStorage 数据类表示的文档执行更新,并将已更新文档作为 NewDataStorage 类型返回。该操作将 releaseDate 字段添加到文档中,且 name 值为 tape:
// Define a data class for returned documentsdata class NewDataStorage( val productName: String, val capacity: Double, val releaseDate: LocalDate)
val filter = Filters.eq(DataStorage::productName.name, "tape")val update = Updates.currentDate("releaseDate")val options = FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
// Specify the class for returned documents as the type parameter in withDocumentClass()val result = collection .withDocumentClass<NewDataStorage>() .findOneAndUpdate(filter, update, options)
println("Updated document: $result")Updated document: NewDataStorage(productName=tape, capacity=5.0, releaseDate=2023-06-15)有关此功能的更多信息,请参阅“数据库和集合”指南中的指定返回类型。
使用注释指定组件转换
本节介绍可用于配置数据类的序列化行为的注解,并提供示例来演示注解行为。
您可以对数据类使用以下注解:
注解名称 | 说明 |
|---|---|
| 将要序列化的属性标记为 |
| 指定将数据类字段转换为 BSON 时的自定义文档字段名称。 |
| 指定 MongoDB 用于存储值的 BSON 类型。只有在需要将值存储为与数据类属性不同的 BSON 类型时,才使用此注解。 警告:如果您在存储为与数据类属性相同类型的属性上包含 |
有关这些属性注解的参考信息,请参阅 org.bson.codecs.pojo.annotations 软件包。
带注解的数据类示例
本节中的代码示例引用以下示例数据类,其中描述了网络设备:
data class NetworkDevice( (BsonType.OBJECT_ID) val deviceId: String, val name: String, ("type") val deviceType: String)插入带注解的数据类
您可以插入 NetworkDevice 实例,如以下代码所示:
val collection = database.getCollection<NetworkDevice>("network_devices")
// Insert the recordval deviceId = ObjectId().toHexString()val device = NetworkDevice(deviceId, "Enterprise Wi-fi", "router")collection.insertOne(device)在 MongoDB 中插入的文档应类似于以下内容:
{ _id: ObjectId("fedc..."), name: 'Enterprise Wi-fi', type: 'router'}检索带注解的数据类
如以下代码所示,您可以按 NetworkDevice 实例检索文档并打印:
val collection = database.getCollection<NetworkDevice>("network_devices")
// Return all documents in the collection as data classesval resultsFlow = collection.find()resultsFlow.collect { println(it) }NetworkDevice(deviceId=645cf..., name=Enterprise Wi-fi, deviceType=router)使用递归类型的操作
驱动程序原生支持对递归定义的数据类进行编码和解码,而不会导致运行时递归。此支持扩展到类型定义中的多个数据类类型的循环。以下代码提供了递归数据类设计的示例:
data class DataClassTree( val content: String, val left: DataClassTree?, val right: DataClassTree?)您可以对递归定义的数据类执行读写操作,就像对其他数据类执行读写操作一样。以下代码显示如何对 DataClassTree 类型的集合执行查找操作:
val collection = database.getCollection<DataClassTree>("myCollection")
val filter = Filters.eq("left.left.right.content", "high german")val resultsFlow = collection.find(filter)resultsFlow.collect { println(it) }DataClassTree(content=indo-european, left=DataClassTree(content=germanic, left=DataClassTree(content=german, left=null, right=DataClassTree(content=high german, ...)), right=...)