Annotation Interface DocumentReference
A
DocumentReference
allows referencing entities in MongoDB using a flexible schema. While the goal is the
same as when using DBRef
, the store representation is different. The reference can be anything, a single
value, an entire Document
, basically everything that can be stored in MongoDB. By default, the
mapping layer will use the referenced entities id value for storage and retrieval.
public class Account { private String id; private Float total; } public class Person { private String id; @DocumentReference private List<Account> accounts; } Account account = ... mongoTemplate.insert(account); template.update(Person.class) .matching(where("id").is(...)) .apply(new Update().push("accounts").value(account)) .first();
lookup()
allows defining a query filter that is independent from the _id field and in combination
with writing converters
offers a flexible way of defining
references between entities.
public class Book { private ObjectId id; private String title; @Field("publisher_ac") @DocumentReference(lookup = "{ 'acronym' : ?#{#target} }") private Publisher publisher; } public class Publisher { private ObjectId id; private String acronym; private String name; @DocumentReference(lazy = true) private List<Book> books; } @WritingConverter public class PublisherReferenceConverter implements Converter<Publisher, DocumentPointer<String>> { public DocumentPointer<String> convert(Publisher source) { return () -> source.getAcronym(); } }
- Since:
- 3.3
- Author:
- Christoph Strobl
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionThe collection the referenced entity resides in.The database the referenced entity resides in.boolean
Controls whether the referenced entity should be loaded lazily.The single document lookup query.A specific sort.
-
Element Details
-
db
String dbThe database the referenced entity resides in. Uses the default database provided byMongoDatabaseFactory
if empty.- Default:
- ""
-
collection
String collectionThe collection the referenced entity resides in. Defaults to the collection of the referenced entity type.- See Also:
- Default:
- ""
-
lookup
String lookupThe single document lookup query. In case of anCollection
orMap
property the individual lookups are combined via an$or
operator.target
points to the source value (or document) stored at the reference property. Properties oftarget
can be used to define the reference query.- Returns:
- an _id based lookup.
- Default:
- "{ \'_id\' : ?#{#target} }"
-
sort
String sortA specific sort.- Default:
- ""
-
lazy
boolean lazyControls whether the referenced entity should be loaded lazily. This defaults to false.- Returns:
- false by default.
- Default:
- false
-