Let's assume we have 2 or more MongoDB documents that are related to each other. When managing them, I'm confused about two approaches. The first approach is to put each other's IDs inside the MongoDB documents on the Java side.
For Example:
@Document
public class Customer {
@MongoId
private String id;
private String addressId;
}
Second Approach: Using DocumentReference
@Document
public class Address {
@MongoId
private String id;
@DocumentReference(lazy = true, lookup = "{ 'primaryAddress' : ?#{#self._id} }")
@ReadOnlyProperty
private Customer customer;
}
@Document
public class Customer {
@MongoId
private String id;
@DocumentReference(lazy = true)
private Address primaryAddress;
}
Which of these two approaches is more performance-efficient? Which one allows me to get the job done with fewer queries? Could you evaluate these in general terms?
When developing with MongoDB and Spring Boot, there are different approaches you can take with regards to storing references between documents:
Storing IDs as Strings:
Using DocumentReference:
Which method is more effective depends on your application's requirements and usage scenarios:
The choice of which approach to adopt will depend on your project's specific requirements and priorities.