简体   繁体   English

具有DBRef的MongoDB / Morphia复合索引

[英]MongoDB/Morphia Compound Index with DBRef

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. 我想在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 线程“主”中的异常com.google.code.morphia.query.ValidationException:验证时无法在“ com.company.test.Team”中找到过去使用“位置”的点符号-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. 感谢您的任何反馈。

If filtering by fields nested inside reference: field access for lists of objects in a class via morphia in mongodb 如果按嵌套在引用内部的字段进行过滤: 通过mongodb中的morphia访问类中的对象列表

If filtering only by id of reference: .filter("location", new Key(Address.class, id)) 如果仅按引用的ID进行过滤:.filter(“ location”,new Key(Address.class,id))

Yes, that's why. 是的,这就是原因。 Your location field is referencing a different collection - ie 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. 您可以选择将“地址”嵌入到团队中-这会将所有内容保存在“团队”集合中,并允许您将“ location.city”索引添加到“团队”类/集合中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM