Add MongoDB example for linking documents.

Closes #631
This commit is contained in:
Christoph Strobl
2021-10-04 13:45:37 +02:00
committed by Greg L. Turnquist
parent a171e33444
commit af124509c8
21 changed files with 1029 additions and 0 deletions

62
mongodb/linking/README.md Normal file
View File

@@ -0,0 +1,62 @@
# Spring Data MongoDB - Linking Example
This project contains examples for linking Documents using:
* `@DBRef`
* `@DocumentReference`
## DBRef
Use MongoDB native `dbref` for storing associations.
```java
class Manager {
@DBRef
private List<Employee> employees;
// ...
}
```
## DocumentReference
Link documents via their id.
```java
class Manager {
@DocumentReference
private List<Employee> employees;
// ...
}
```
Link documents via a (non _id_) property.
```java
class Manager {
@DocumentReference(lookup = "{ 'name' : ?#{#target} }")
private List<Employee> employees;
// ...
}
```
Link documents JPA style.
```java
class Manager {
@ReadOnlyProperty
@DocumentReference(lookup="{'managerId':?#{#self._id} }")
private List<Employee> employees;
// ...
}
```
----
For more information please refer to the [Reference Documentation](https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-usage-references).