Derived Queries not working with DBRef

2020-07-23 08:51发布

问题:

I am using Mongo with Spring Data.

I have two (first class) entities (@Documents) Entity1 and Entity2, where Entity1 has a reference (@DBRef) of Entity2 within it. Everything works fine, but when executing derived queries such as :

public List<Entity1> findByEntity2Property1(String property1)

The above query returns no results although there are documents with the given query params. Why is that?

回答1:

I don't think the query can work as you expect it to work as MongoDB pretty much does not allow restricting results on properties of related documents pointed to via a DBRef. So you essentially have to query all Entity2 instances matching the given value on property Property1 and then query for Entity1 instances matching the DBRefed property against the results of the first query.