2012-02-21 66 views
0

我还没有找到明确的答案,我希望有人能帮助我。我想在Mongo中“引用”的对象上创建一个复合索引。我显然遇到了一个错误,我将在代码片段下面描述。MongoDB/Morphia化合物索引与DBRef

@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”中找到,而验证 - location.city

所以我想我的问题是:我会得到这个错误,因为“地址”是内“团队”的引用或我错过了别的吗?

感谢您的任何反馈意见。

回答

0

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