“文档” 菜单
文档首页
/ / /
Java (Sync) 驱动程序
/ /

文档数据格式:记录

在此页面上

  • 概述
  • 对记录进行序列化和反序列化
  • 记录示例
  • 插入记录
  • 检索记录
  • 使用注释指定组件转换
  • 带注释的记录示例
  • 插入注释记录
  • 检索注释记录
  • 使用递归类型的操作
  • 检索记录编解码器

在本指南中,您可以了解如何使用 Java 记录在 MongoDB Java 驱动程序中存储和检索数据。Java 记录是一种 Java 类,通常用于对数据建模并将业务逻辑与数据表示分开。

提示

您可以在 Java 16或更高版本中声明 Java 记录。 通过 Java17 语言更新:记录类,了解有关记录的功能和限制的更多信息。

如果您使用的是早期版本的 Java,则可以改用普通的旧 Java 对象。有关实施详情,请参阅文档数据格式:POJO指南。

注意

如果您在 OSGi 容器中使用该驱动程序,并且您的应用程序使用该驱动程序对 Java 记录进行编码或解码,则必须添加对 org.bson.codecs.record模块的显式依赖项。 在 IBM OSGi 文档中了解有关为 OSGi 容器定义依赖项的更多信息。

该驱动程序本身支持使用默认编解码器注册表对 MongoDB 读写操作的 Java 记录进行编码和解码。默认编解码器注册表是称为编解码器的类的集合,它们定义如何转换编码和解码 Java 类型。在编解码器指南中了解有关编解码器和默认编解码器注册表的详情。

本部分的代码示例引用了以下示例记录,其中描述了数据存储设备:

public record DataStorageRecord(
String productName,
double capacity
) {}

您可以插入 DataStorageRecord 实例,如以下代码所示:

MongoCollection<DataStorageRecord> collection = database.getCollection("data_storage_devices", DataStorageRecord.class);
// insert the record
collection.insertOne(new DataStorageRecord("2TB SSD", 1.71));

如以下代码所示,您可以按 DataStorageRecord 实例检索文档并打印:

MongoCollection<DataStorageRecord> collection = database.getCollection("data_storage_devices", DataStorageRecord.class);
// retrieve and print the records
List<DataStorageRecord> records = new ArrayList<DataStorageRecord>();
collection.find().into(records);
records.forEach(System.out::println);
DataStorageRecord[productName=1TB SSD, capacity=1.71]

本节介绍如何使用注解配置记录组件的序列化行为。有关支持的注解的完整列表,请参阅 org.bson.codecs.pojo.annotations 包 API 文档。

注意

驱动程序支持 Java 记录的注释,但前提是在定义组件时包含注释,如以下记录示例所示。您不能在记录构造函数中使用注释。

本部分的代码示例引用以下示例记录,其中描述了网络设备:

import org.bson.BsonType;
import org.bson.codecs.pojo.annotations.BsonProperty;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonRepresentation;
public record NetworkDeviceRecord(
@BsonId()
@BsonRepresentation(BsonType.OBJECT_ID)
String deviceId,
String name,
@BsonProperty("type")
String deviceType
)
{}

您可以插入 DataStorageRecord 实例,如以下代码所示:

MongoCollection<NetworkDeviceRecord> collection = database.getCollection("network_devices", NetworkDeviceRecord.class);
// insert the record
String deviceId = new ObjectId().toHexString();
collection.insertOne(new NetworkDeviceRecord(deviceId, "Enterprise Wi-fi", "router"));

在 MongoDB 中插入的文档类似于以下内容:

{
_id: ObjectId("fedc..."),
name: 'Enterprise Wi-fi',
type: 'router'
}

如以下代码所示,您可以按 NetworkDeviceRecord 实例检索文档并打印:

MongoCollection<NetworkDeviceRecord> collection = database.getCollection("network_devices", NetworkDeviceRecord.class);
// return all documents in the collection as records
List<NetworkDeviceRecord> records = new ArrayList<NetworkDeviceRecord>();
collection.find().into(records);
records.forEach(System.out::println);
NetworkDeviceRecord[deviceId=fedc..., name=Enterprise Wi-fi, deviceType=router]

注意

org.bson.codecs.records.annotations 包已弃用。请使用 org.bson.codecs.pojo.annotations 包中的相应注解。

驱动程序本身支持对递归定义的记录进行编码和解码,而不会导致运行时递归。这种支持扩展到在类型定义的多个记录类型的循环。以下代码提供了递归记录设计的示例:

public record RecordTree(
String content,
RecordTree left,
RecordTree right
) {}

就像对其他记录一样,您可以对递归定义的记录执行读写操作。以下代码显示如何对 RecordTree 类型的集合执行查找操作:

MongoDatabase database = mongoClient.getDatabase("myDB");
MongoCollection<RecordTree> collection = database.getCollection("myCollection", RecordTree.class);
Bson filter = Filters.eq("left.left.right.content", "Ikatere");
collection.find(filter).forEach(doc -> System.out.println(doc));
RecordTree[content=Ranginui, left=RecordTree[content=..., left=RecordTree[content=..., right=RecordTree[content=Ikatere...]]

您可以使用 RecordCodecProvider 来检索记录编解码器。如果您想自定义编解码器,将 Java 记录对象与相应的 BSON 类型进行编码和解码,同时尽量减少重复代码,则应使用此接口。要了解有关编解码器及其使用的详情,请参阅编解码器

您无法直接创建记录编解码器,但可使用 RecordCodecProvider 在代码中实现记录编解码器。有关 RecordCodecProvider 的更多信息,请参阅 API 文档

← 文档数据格式:POJO