![](/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.