bwoodson
bwoodson

Reputation: 70

SpringData MongoDB findBy Nested Property (non ID) Using DBRef

Using SpringData MongoDB (spring-data-mongodb 1.9.1.RELEASE) I am needing to query a User based on a User's Role linked by @DBRef.

User

@Document(collection = "user")
public class User {
    private String userName;
    private boolean isActive;  
    @DBRef
    private List<Role> roles;
}

Role

@Document(collection = "role")
public class Role {
    private String roleName;
    private String description;
    private long roleNum;
} 

User Repository

@Repository
public interface UserRepository extends MongoRepository<User, String> {

    public User findByUserName(String username);

    @Query(value = "{'roles.$roleName' : ?0}")
    public List<User> findByRolesRoleName(String roleName);
}

A question similar has been asked, but not answered. Makes me think that maybe this type of findBy is not supported.

This seems fairly straight forward, however, the results of findByRolesRoleName is always an empty list (size = 0).

Has anyone gotten a findBy for this type of relationship working properly?

Upvotes: 2

Views: 1061

Answers (1)

Christoph Strobl
Christoph Strobl

Reputation: 6736

It is not possible to query on non id properties of DBRef in MongoDB itself. Thus it is not possible to do so using Spring Data MongoDB.

Upvotes: 4

Related Questions