Reputation: 13
I've recently been playing with query creation from method name. I have this interface that extends MongoRepository:
public interface CompanyRepository extends MongoRepository<Company, String> {
//this works.
Company findByEmployeename(String name);
//this doesn't
List<Company> findByEmployeename(List<String> name);
}
I was hoping I could get back list of companies from a list of employees, but when I call the method I get back a empty list. I don't want to use findByEmployeename(String name)
in a loop or some sort. Is this possible with a custom repository method?
Upvotes: 1
Views: 507
Reputation: 5856
Yes. Just append In
to List<Company> findByEmployeenameIn(List<String> name);
, which translates to $in
operator in Mongo query. You should just read up on https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.
If you wanna see the the corresponding query created by your method in the console logs. In your properties/yml file, add logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
Upvotes: 1