spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

Field-Selection using Pageable [DATAMONGO-184] #1119

Closed spring-projects-issues closed 5 years ago

spring-projects-issues commented 13 years ago

Robert Stiller opened DATAMONGO-184 and commented

It would be helpful to have the option for field-selection using a org.springframework.data.domain.Pageable.

I guess the best way to do it is to extend org.springframework.data.domain.PageRequest (may named "MongoPageRequest") with the extra option for the fields, so queries like that are possible:

List<MyObjectWithVeryFewData> list = myRepository.findAll(new MongoPageRequest(
    0, // page
    10, // size
    new Sort("name"), // sort
    Arrays.asList("name", "attributes.myOption") // selects only the name and myOption of the attributes ...
));

No further details from DATAMONGO-184

spring-projects-issues commented 13 years ago

Oliver Drotbohm commented

I'm a bit hesitant to extend the semantics of a Pageable into the field spec area as field specs absolutely don't correlate to pagination at all. So what you're generally trying to achieve is to be able to pass field specs to query methods dynamically, right? You might want to watch issue DATAMONGO-103 for a Querydsl based approach to that but we probably add a general purpose mechanism to dynamically hand field specs to query methods. I thought of something alongside

public UserRepository extends Repository<User, Long> {

  List<User> findByFirstname(String firstname, @FieldSpec DBObject spec);
}

We might support DBObject, Collection instances or plain arrays and varags for the @FieldSpec annotation. We could also add special CRUD methods to MongoRepository interface for findAll and findOne overloads although these methods can actually be added to the repository interface as needed by declaring an empty @Query annotation once the general infrastructure is in place.

public UserRepository extends Repository<User, Long> {

  @Query("{}")
  List<User> findAll(@FieldSpec DBObject spec);
}

What do you think?

spring-projects-issues commented 13 years ago

Robert Stiller commented

The QueryDSL approch sounds good - but QueryDSL is needed, which would be a little bit invasiv...

I would prefer a simple array/list of Strings or a org.springframework.data.mongodb.query.Field object

spring-projects-issues commented 5 years ago

Mark Paluch commented

That's available via the @Field annotation (see reference documentation) and interface projections