0

我一直无法找到明确的答案,希望有人可以帮助我。我想在 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

所以我想我的问题是:我收到这个错误是因为“地址”是“团队”中的引用还是我错过了其他东西?

感谢您的任何反馈。

4

2 回答 2

0

如果按嵌套在引用中的字段进行过滤: 通过 mongodb 中的 morphia 对类中对象列表的字段访问

如果仅按参考 id 过滤: .filter("location", new Key(Address.class, id))

于 2014-11-13T16:17:29.173 回答
0

是的,这就是为什么。您的位置字段引用了不同的集合 - 即“地址”集合中的“城市”字段。您可以选择在团队中嵌入地址 - 这将保存团队集合中的所有内容,并让您将“location.city”索引添加到“团队”类/集合中。

于 2012-02-29T17:50:49.263 回答