【问题标题】:MongoDB/Morphia Compound Index with DBRef带有 DBRef 的 MongoDB/Morphia 复合索引
【发布时间】:2012-02-21 18:59:58
【问题描述】:

我还没有找到明确的答案,希望有人能帮助我。我想在 Mongo 中“引用”的对象上创建一个复合索引。我显然遇到了一个错误,我将在下面的代码 sn-ps 进行描述。

@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”中找到“位置”之后的点符号。城市

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

感谢您的任何反馈。

【问题讨论】:

    标签: java mongodb morphia


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果按嵌套在引用中的字段进行过滤: field access for lists of objects in a class via morphia in mongodb

      如果仅通过引用的 id 过滤:.filter("location", new Key(Address.class, id))

      【讨论】:

        猜你喜欢
        • 2016-04-17
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 2020-05-11
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 2012-02-01
        相关资源
        最近更新 更多