Query DBRef with Spring Data MongoDB using QueryDSL
I am using Spring Data MongoDB and QueryDSL to do some simple queries, but I am having problems trying to use a predicate with a DBRef object field.
It seems that DBRefs are not allowed, so the query always returns empty results. There are some questions on this topic since 2014 mostly, and although there seems to be some work done on it on both the QueryDSL side and the w760> side, I still can't get it to work and haven't found any working example.
I am looking for a simple solution like in the following simplified case:
@Document
class Foo {
@Id Integer id;
@DBref Bar bar;
}
@Document
class Bar {
@Id Integer id;
String name;
}
interface FooRepository extends MongoRepository<Foo, Integer>, QueryDslPredicateExecutor<Foo> { ... }
and the query I'm trying to use:
fooRepository.findAll(QFoo.foo.bar.name.eq("test"))
I am using QueryDSL 4.1.4, Spring Boot 1.5.3 with Spring Data MongoDB 1.10.3
Is this supported? Did I miss something?
source to share
TL; DR;
Not supported.
Description
You are expressing a predicate that follows a link to another document.
MongoDB is a document-oriented database that is connectionless. Each document can contain subdocuments or links (which are artificial). In addition, each document must either be viewed individually or selected as a collection of documents based on a request.
Since there are no joins in MongoDB, you cannot query for references DBRef
. You can express a query that scans a subdocument as the subdocument is nested.
Spring Data MongoDB provides the functionality provided by MongoDB in a more user-friendly way and does not purport to provide functionality that MongoDB supports.
Since MongoDB 3.4, the aggregation framework supports $graphLookup
that MongoDB can search during the aggregation stage to find individual documents. Search terms are expensive and something you would rather avoid.
source to share