Creating custom finders

With Spring Data repositories, we are able to create queries to suit any situation. Earlier in this chapter, we saw findByName, which merely queries based on the domain object's name attribute.

The following table shows a more comprehensive collection of finders we can write with Spring Data MongoDB. To illustrate the breadth of these keywords, it presumes a domain model bigger than the Image class we defined earlier:

Finder Method

Description

findByLastName(...​)

Query based on lastName

findByFirstNameAndLastName(...​)

Query based on firstName and lastName

findByFirstNameAndManagerLastName(...​)

Query based on firstName and by a related manager's lastName

findTop10ByFirstName(...​) or ...

Get Learning Spring Boot 2.0 - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.