Spring Data MongoDB findBy Nested 属性 (non ID) 使用 DBRef

SpringData MongoDB findBy Nested Property (non ID) Using DBRef

使用 SpringData MongoDB (spring-data-mongodb 1.9.1.RELEASE) 我需要根据@DBRef 链接的用户角色查询用户.

用户

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

角色

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

用户存储库

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

    public User findByUserName(String username);

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

有人问 question similar,但没有回答。让我觉得可能不支持这种类型的 findBy。

这看起来相当简单,但是,findByRolesRoleName 的结果始终是一个空列表(大小 = 0)。

有没有人为这种类型的关系正常工作找到了 findBy?

无法在 MongoDB 本身中查询 DBRef 的非 id 属性。因此,使用 Spring 数据 MongoDB 无法做到这一点。