1
votes

Is it possible to limit the fields which are projected in findAll method using Spring Data Rest mongo repository. If so how can I do it ?

For example, class A has name, age, address, mobileNo.

findAll will return List

What I want is, just name and age which need to be returned in List< List< String>>>. Do I need to give custom implementation for this or this can be done by giving @Query("{}, {name: 1, age: 1}")

2
I faced the same challenge too. I used PagingAndSortingRepository.CodeBender

2 Answers

0
votes

Field restriction can be done.Like this:

@Query(value="{ 'name' : ?0 }", fields="name,age")
List <Person> findByThePersonsName(String name);

This will return only the name and age properties of the Person objects ie, address and mobileNo will be null.

0
votes

You have an error in the code, should be:

@Query(value="{ 'name' : ?0 }", fields=" 'name' : 1, 'age' :1")
List <Person> findByThePersonsName(String name);