ufasoli
ufasoli

Reputation: 1068

Projection on a MongoDb Query using Spring data and QueryDSL

I have a Spring MVC/Spring Data / Mongo DB application. I have setted up my environement according the the spring data documentation and my repositories work fine (I can execute queries with predicates)

I was wondering if it was possible to execute a type safe query (using Spring Data and QueryDSL) while making a projection (I want only a few fields of a very big document).

The QueryDSL documentation gives an example for Hibernate but states it can be done in all modules QueryDSL Documentation (but I haven't been able to find out how to do it with Mongo)

here's the code snippet for hibernate

class CustomerDTO {

  @QueryProjection
  public CustomerDTO(long id, String name){
     ...
 }

 QCustomer customer = QCustomer.customer;
 JPQLQuery query = new HibernateQuery(session);
 List<CustomerDTO> dtos = qry.from(customer).list(new QCustomerDTO(customer.id,    customer.name));     

Any Ideas ?

Upvotes: 6

Views: 4169

Answers (2)

Marco Camacho
Marco Camacho

Reputation: 11

I've just built a projection like this:

Criteria c1 = Criteria.where("field.name").is("val")
Criteria projection = Criteria.where("field").is(1)
BasicQuery query = new BasicQuery(c1.getCriteriaObject(), projection.getCriteriaObject())

Upvotes: 0

Timo Westk&#228;mper
Timo Westk&#228;mper

Reputation: 22180

This is currently not supported. Feel free to add a ticket for it into our Issue tracker.

The Lucene and Mongodb modules of Querydsl support only direct projections from the query root, but for custom projections something could be figured out.

Upvotes: 2

Related Questions