文档菜单
文档首页
/ / /
Java 同步驱动器
/ /

文档数据格式:记录

在本页

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

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

提示

您可以在Java 16或更高版本中声明Java记录。有关记录的功能和限制的更多信息,请参阅Java 17语言更新:记录类.

如果您正在使用Java的更早版本,则可以使用普通的Java对象。有关实现细节,请参阅文档数据格式:POJOs指南

注意

如果您在OSGi容器中使用驱动程序,并且您的应用程序使用驱动程序来编码或解码Java记录,则必须添加对org.bson.codecs.record模块的显式依赖。有关在OSGi容器中定义依赖项的更多信息,请参阅IBM 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文档。

返回

POJOs