我仍在尝试了解mongodb如何映射最佳实体。例如:实体用户和实体地址。可能会one-to-many有人来自jpa背景。在 mongo 我不想使用dbref. 所以地址Set在用户的集合中。
假设我正在使用spring-data-mongo:
问题1:用户和地址都应该有@Document注释吗?还是只有用户?
问题2:查询用户地址的最佳方法是什么。一开始有可能吗?因为现在我查询得到Userby usernameorId然后得到用户的地址。我可以直接查询sub-document吗?如果是,如何使用spring-data-mongoCriteria Query 完成:
@Document
public class User{
@Id
private Long ID;
private String username;
private Set<Address> addresses = new HashSet<Address>();
...
}
@Document
public class Address {
@Id
private Long ID;
private String city;
private String line1;
...
}