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.
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.