MongoDB/Morphia 复合索引与 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;
}
我得到的错误是:
线程“main”com.google.code.morphia.query.ValidationException 中出现异常:验证位置时,无法在“com.company.test.Team”中找到过去的“位置”,无法使用点符号。城市
所以我我猜我的问题是:我收到此错误是因为“地址”是“团队”内的引用还是我遗漏了其他内容?
感谢您的任何反馈。
I haven't been able to find a definitive answer and I hope someone can help me. I want to create a compound index on an object that is "referenced" within Mongo. I'm obviously getting an error, which I'll describe below the code snippets.
@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;
}
And the error I'm getting is:
Exception in thread "main" com.google.code.morphia.query.ValidationException: Can not use dot-notation past 'location' could not be found in 'com.company.test.Team' while validating - location.city
So I guess my question is: am I getting this error because "Address" is a reference within "Team" or am I missing something else?
Thanks for any feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这就是原因。您的位置字段引用了不同的集合 - 即“地址”集合中的“城市”字段。您可以选择将地址嵌入团队内 - 这将保存团队集合中的所有内容,并让您将“location.city”索引添加到“团队”类/集合中。
Yes, that's why. Your location field is referencing a different collection - i.e. the "city" field in in a "Address" collection. You have the option of embedding Address inside team - this will save everything in the Team collection, and let you add your "location.city" index to the "Team" class/collection.
如果按引用内嵌套的字段进行过滤:
对象列表的字段访问在 mongodb 中通过吗啡在类中
如果仅通过引用的 id 进行过滤: .filter("location", new Key(Address.class, id))
If filtering by fields nested inside reference:
field access for lists of objects in a class via morphia in mongodb
If filtering only by id of reference: .filter("location", new Key(Address.class, id))