![](/img/trans.png)
[英]How do I create a compound unique index in Morphia/MongoDB Java?
[英]MongoDB/Morphia Compound Index with DBRef
我还没有找到确切的答案,希望有人能帮助我。 我想在Mongo中“引用”的对象上创建复合索引。 我显然遇到了错误,我将在代码片段下面进行描述。
@Entity
public class Address {
public Address (String street, String City, String state, String zip) {
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
// Getters and Setters
@Id private ObjectId id;
private String street;
private String city;
private String state;
private String zip;
}
@Entity
@Indexes( @Index("location.city, name") )
public class Team {
public Team (String sport, String name, Address location) {
this.sport = sport;
this.name = name;
this.location = location;
}
// Getters and Setters
@Id private ObjectId id;
private String sport;
private String name;
@Reference private Address location;
@Reference private List<Player> players;
}
我得到的错误是:
线程“主”中的异常com.google.code.morphia.query.ValidationException:验证时无法在“ com.company.test.Team”中找到过去使用“位置”的点符号-location.city
所以我想我的问题是:是因为“地址”是“团队”中的引用,还是出现其他错误?
感谢您的任何反馈。
如果按嵌套在引用内部的字段进行过滤: 通过mongodb中的morphia访问类中的对象列表
如果仅按引用的ID进行过滤:.filter(“ location”,new Key(Address.class,id))
是的,这就是原因。 您的位置字段引用了一个不同的集合-即“地址”集合中的“城市”字段。 您可以选择将“地址”嵌入到团队中-这会将所有内容保存在“团队”集合中,并允许您将“ location.city”索引添加到“团队”类/集合中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.