Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query DBRef with Spring Data MongoDB using QueryDSL

I'm using Spring Data MongoDB and QueryDSL to perform some simple queries, but I'm having issues trying to use a predicate with a DBRef object's field.

It seems the DBRef are not resolved, so the query always returns empty results. There are some questions about this topic from 2014 mostly, and although there seem to have been some work done about it on both QueryDSL and Spring Data's side I still can't make it work and didn't find any working example.

I'm looking for a simple solution, as in the following simplified test 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'm using QueryDSL 4.1.4, Spring Boot 1.5.3 with Spring Data MongoDB 1.10.3

Is this supported? Am I missing anything?

like image 843
Christophe Douy Avatar asked Oct 30 '22 07:10

Christophe Douy


1 Answers

TL;DR;

It's not supported.

Explanation

You're expressing a predicate that follows the reference into a different document.

MongoDB is a document-oriented database that knows no joins. Each document may contain subdocuments or references (which are sort of artificial). Also, each document has to be either looked up individually or fetched as a collection of documents based on a query.

Because there are no joins built-in to MongoDB you can't query across DBRef references. You can express a query that scans the subdocument as the subdocument is embedded.

Spring Data MongoDB exposes functionality provided by MongoDB in a more user-friendly way and does not pretend to provide functionality that isn's supported by MongoDB.

Since MongoDB 3.4, the aggregation framework supports $graphLookup to let MongoDB lookup during the aggregation stage to look up individual documents. Lookups are expensive and that's something you'd rather want to avoid.

like image 161
mp911de Avatar answered Nov 15 '22 09:11

mp911de