文档数据格式:数据类
概述
在本指南中,您可以学习如何使用 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 classes val resultsFlow = collection.find() resultsFlow.collect { println(it) }
DataStorage(productName=tape, capacity=5.0)
您可以为从集合返回的文档指定一个类,即使它与检索集合时指定的类不同。
以下示例对上一个示例中由DataStorage
数据类表示的文档进行更新,并将更新后的文档作为NewDataStorage
类型返回。此操作向具有name
值为tape
的文档添加releaseDate
字段
// Define a data class for returned documents data 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)
有关此功能的更多信息,请参阅指定返回类型在数据库和集合指南中。
使用注解指定组件转换
本节描述了您可以使用注解来配置数据类的序列化行为,并提供了一个示例来演示注解的行为。
您可以在数据类中使用以下注解
注解名称 | 描述 |
---|---|
BsonId | 标记一个属性以序列化为其 _id 属性。 |
BsonProperty | 在将数据类字段转换为 BSON 时指定自定义文档字段名称。 |
BsonRepresentation | 指定 MongoDB 使用的 BSON 类型来存储值。仅在您需要将值存储为与数据类属性不同的 BSON 类型时使用此注解。 警告:如果将 |
有关这些属性注解的参考信息,请参阅org.bson.codecs.pojo.annotations 包。
示例注解数据类
本节中的代码示例参考以下示例数据类,它描述了一个网络设备
data class NetworkDevice( val deviceId: String, val name: String, val deviceType: String )
插入一个注解数据类
您可以将 NetworkDevice
实例插入,如下面的代码所示
val collection = database.getCollection<NetworkDevice>("network_devices") // Insert the record val 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 classes val 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=...)