Projection on a MongoDb Query using Spring data and QueryDSL

柔情痞子 提交于 2019-12-22 10:45:32

问题


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 ?


回答1:


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.




回答2:


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())


来源:https://stackoverflow.com/questions/11688807/projection-on-a-mongodb-query-using-spring-data-and-querydsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!