对于最新的稳定版本,请使用 Spring Data MongoDB 4.5.2! |
使用 DBRefs
映射框架不必存储嵌入在文档中的子对象。
您也可以将它们单独存储并使用DBRef
以参考该文档。
当从 MongoDB 加载对象时,这些引用会被急切解析,以便您返回一个映射对象,该对象看起来与嵌入在顶级文档中存储的对象相同。
以下示例使用 DBRef 来引用独立于引用它的对象而存在的特定文档(为了简洁起见,这两个类都内联显示):
@Document
public class Account {
@Id
private ObjectId id;
private Float total;
}
@Document
public class Person {
@Id
private ObjectId id;
@Indexed
private Integer ssn;
@DBRef
private List<Account> accounts;
}
您无需使用@OneToMany
或类似的机制,因为对象列表告诉映射框架你想要一对多关系。
当对象存储在MongoDB中时,有一个DBRefs列表,而不是Account
对象本身。
当涉及到加载集合时DBRef
建议将集合类型中保存的引用限制为特定的MongoDB集合。
这允许批量加载所有引用,而指向不同MongoDB集合的引用需要一一解析。
映射框架不处理级联保存。
如果您将Account 由Person 对象,则必须将Account 对象。
叫save 在Person 对象不会自动保存Account 对象中的accounts 财产。 |
DBRef
s 也可以延迟解析。
在这种情况下,实际的Object
或Collection
引用的数量在首次访问属性时解析。
使用lazy
属性@DBRef
来指定这一点。
也定义为延迟加载的必需属性DBRef
并用作构造函数的参数也用延迟加载代理进行装饰,确保对数据库和网络的压力尽可能小。
延迟加载DBRef 可能很难调试。
确保工具不会意外触发代理解析,例如调用toString() 或调用属性 getter 的一些内联调试渲染。
请考虑启用跟踪日志记录org.springframework.data.mongodb.core.convert.DefaultDbRefResolver 深入了解DBRef 分辨率。 |
延迟加载可能需要类代理,而类代理又可能需要访问 JDK 内部,这些内部结构从 Java 16+ 开始是未开放的,因为 JEP 396:默认强封装 JDK 内部。
对于这些情况,请考虑回退到接口类型(例如,从ArrayList 自List )或提供所需的--add-opens 论点。 |
使用文档引用
用@DocumentReference
提供了一种灵活的方式来引用 MongoDB 中的实体。
虽然目标与使用 DBRefs 时相同,但存储表示形式不同。DBRef
解析为具有固定结构的文档,如 MongoDB 参考文档中所述。
文档引用,不遵循特定格式。
它们实际上可以是任何东西,单个值,整个文档,基本上可以存储在 MongoDB 中的所有内容。
默认情况下,映射层将使用引用的实体 id 值进行存储和检索,如下面的示例所示。
@Document
class Account {
@Id
String id;
Float total;
}
@Document
class Person {
@Id
String id;
@DocumentReference (1)
List<Account> accounts;
}
Account account = …
template.insert(account); (2)
template.update(Person.class)
.matching(where("id").is(…))
.apply(new Update().push("accounts").value(account)) (3)
.first();
{
"_id" : …,
"accounts" : [ "6509b9e" … ] (4)
}
1 | 标记集合Account 要引用的值。 |
2 | 映射框架不处理级联保存,因此请确保单独保留引用的实体。 |
3 | 添加对现有实体的引用。 |
4 | 引用Account 实体表示为其_id 值。 |
上面的示例使用_id
-基于获取查询 ({ '_id' : ?#{#target} }
) 进行数据检索,并急切地解析链接的实体。
可以使用@DocumentReference
属性 | 描述 | 默认值 |
---|---|---|
|
集合查找的目标数据库名称。 |
|
|
目标集合名称。 |
带注释的属性的域类型,分别是值类型(如果为 |
|
通过 SpEL 表达式评估占位符的单个文档查找查询 |
一 |
|
用于在服务器端对结果文档进行排序。 |
默认情况下无。
结果顺序 |
|
如果设置为 |
默认情况下,急切解析属性。 |
延迟加载可能需要类代理,而类代理又可能需要访问 JDK 内部,这些内部结构从 Java 16+ 开始是未开放的,因为 JEP 396:默认强封装 JDK 内部。
对于这些情况,请考虑回退到接口类型(例如,从ArrayList 自List )或提供所需的--add-opens 论点。 |
@DocumentReference(lookup)
允许定义可以与_id
字段,因此提供了一种灵活的方式来定义实体之间的引用,如下面的示例所示,其中Publisher
的书由其首字母缩略词而不是内部引用id
.
@Document
class Book {
@Id
ObjectId id;
String title;
List<String> author;
@Field("publisher_ac")
@DocumentReference(lookup = "{ 'acronym' : ?#{#target} }") (1)
Publisher publisher;
}
@Document
class Publisher {
@Id
ObjectId id;
String acronym; (1)
String name;
@DocumentReference(lazy = true) (2)
List<Book> books;
}
Book
公文{
"_id" : 9a48e32,
"title" : "The Warded Man",
"author" : ["Peter V. Brett"],
"publisher_ac" : "DR"
}
Publisher
公文{
"_id" : 1a23e45,
"acronym" : "DR",
"name" : "Del Rey",
…
}
1 | 使用acronym 字段查询Publisher 收集。 |
2 | 延迟加载回对Book 收集。 |
上面的代码片段显示了使用自定义引用对象时的读取方面。
写入需要一些额外的设置,因为映射信息没有表达在哪里#target
源于。
映射层需要注册Converter
在目标文档和DocumentPointer
,如下所示:
@WritingConverter
class PublisherReferenceConverter implements Converter<Publisher, DocumentPointer<String>> {
@Override
public DocumentPointer<String> convert(Publisher source) {
return () -> source.getAcronym();
}
}
如果没有DocumentPointer
转换器,则可以根据给定的查找查询计算目标参考文档。
在这种情况下,关联目标属性的评估如下例所示。
@Document
class Book {
@Id
ObjectId id;
String title;
List<String> author;
@DocumentReference(lookup = "{ 'acronym' : ?#{acc} }") (1) (2)
Publisher publisher;
}
@Document
class Publisher {
@Id
ObjectId id;
String acronym; (1)
String name;
// ...
}
{
"_id" : 9a48e32,
"title" : "The Warded Man",
"author" : ["Peter V. Brett"],
"publisher" : {
"acc" : "DOC"
}
}
1 | 使用acronym 字段查询Publisher 收集。 |
2 | 查找查询的字段值占位符(如acc ) 用于形成参考文档。 |
还可以使用以下组合对关系样式一对多引用进行建模@ReadonlyProperty
和@DocumentReference
.
这种方法允许链接类型不将链接值存储在所属文档中,而是存储在引用文档上,如下面的示例所示。
@Document
class Book {
@Id
ObjectId id;
String title;
List<String> author;
ObjectId publisherId; (1)
}
@Document
class Publisher {
@Id
ObjectId id;
String acronym;
String name;
@ReadOnlyProperty (2)
@DocumentReference(lookup="{'publisherId':?#{#self._id} }") (3)
List<Book> books;
}
Book
公文{
"_id" : 9a48e32,
"title" : "The Warded Man",
"author" : ["Peter V. Brett"],
"publisherId" : 8cfb002
}
Publisher
公文{
"_id" : 8cfb002,
"acronym" : "DR",
"name" : "Del Rey"
}
1 | 设置链接Book (参考)至Publisher (所有者)通过存储Publisher.id 在Book 公文。 |
2 | 将保存引用的属性标记为只读。
这可以防止存储对单个Book s 替换为Publisher 公文。 |
3 | 使用#self 变量来访问Publisher 文档和在此检索中Books 与匹配publisherId . |
有了上述所有内容,就可以对实体之间的各种关联进行建模。 查看下面的非详尽示例列表,以了解什么是可能的。
class Entity {
@DocumentReference
ReferencedObject ref;
}
// entity
{
"_id" : "8cfb002",
"ref" : "9a48e32" (1)
}
// referenced object
{
"_id" : "9a48e32" (1)
}
1 | MongoDB简单类型可以直接使用,无需进一步配置。 |
class Entity {
@DocumentReference(lookup = "{ '_id' : '?#{#target}' }") (1)
ReferencedObject ref;
}
// entity
{
"_id" : "8cfb002",
"ref" : "9a48e32" (1)
}
// referenced object
{
"_id" : "9a48e32"
}
1 | target 定义了参考值本身。 |
refKey
字段用于查找查询class Entity {
@DocumentReference(lookup = "{ '_id' : '?#{refKey}' }") (1) (2)
private ReferencedObject ref;
}
@WritingConverter
class ToDocumentPointerConverter implements Converter<ReferencedObject, DocumentPointer<Document>> {
public DocumentPointer<Document> convert(ReferencedObject source) {
return () -> new Document("refKey", source.id); (1)
}
}
// entity
{
"_id" : "8cfb002",
"ref" : {
"refKey" : "9a48e32" (1)
}
}
// referenced object
{
"_id" : "9a48e32"
}
1 | 用于获取参考值的键必须是写入期间使用的键。 |
2 | refKey 是target.refKey . |
class Entity {
@DocumentReference(lookup = "{ 'firstname' : '?#{fn}', 'lastname' : '?#{ln}' }") (1) (2)
ReferencedObject ref;
}
// entity
{
"_id" : "8cfb002",
"ref" : {
"fn" : "Josh", (1)
"ln" : "Long" (1)
}
}
// referenced object
{
"_id" : "9a48e32",
"firstname" : "Josh", (2)
"lastname" : "Long", (2)
}
1 | 读/写密钥fn & ln 基于查找查询的链接文档。 |
2 | 使用非 id 字段查找目标文档。 |
class Entity {
@DocumentReference(lookup = "{ '_id' : '?#{id}' }", collection = "?#{collection}") (2)
private ReferencedObject ref;
}
@WritingConverter
class ToDocumentPointerConverter implements Converter<ReferencedObject, DocumentPointer<Document>> {
public DocumentPointer<Document> convert(ReferencedObject source) {
return () -> new Document("id", source.id) (1)
.append("collection", … ); (2)
}
}
// entity
{
"_id" : "8cfb002",
"ref" : {
"id" : "9a48e32", (1)
"collection" : "…" (2)
}
}
1 | 读/写密钥_id from/to 引用文档以在查找查询中使用它们。 |
2 | 可以使用其键从参考文档中读取集合名称。 |
我们知道在查找查询中使用各种 MongoDB 查询运算符很诱人,这很好。 但有几个方面需要考虑:
一些更笼统的评论:
|