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 findFirst10ByFirstName(...​)

Query based on firstName, but only return the first ten entries

findByFirstNameIgnoreCase(...​)

Query by firstName, but ignore the case of the text

findByFirstNameAndLastNameAllIgnoreCase(...​)

Query by firstName and lastName, but ignore the case of the text in ALL fields

findByFirstNameOrderByLastNameAsc(...​)

Query by firstName, but order the results based on lastName in ascending order (or use Desc for descending order)

findByBirthdateAfter(Date date)

Query based on birthdate being after the date

findByAgeGreaterThan(int age)

Query based on age attribute being greater than age parameter.

findByAgeGreaterThanEqual(int age)

Query based on age attribute being greater than or equal to age parameter.

findByBirthdateBefore(Date date)

Query based on birthdate being before the date

findByAgeLessThan(int age)

Query based on age attribute being less than age parameter.

findByAgeLessThanEqual(int age)

Query based on age attribute being less than or equal to age parameter.

findByAgeBetween(int from, int to)

Query based on age being between from and to

findByAgeIn(Collection ages)

Query based on age being found in the supplied collection

findByAgeNotIn(Collection ages)

Query based on age NOT being found in the supplied collection

findByFirstNameNotNull() or findByFirstNameIsNotNull()

Query based on firstName not being null

findByFirstNameNull() or findByFirstNameIsNull()

Query based on firstName being null

findByFirstNameLike(String f) or findByFirstNameStartingWith(String f) or findByFirstNameEndingWith(String f)

Query based on input being a regular expression

findByFirstNameNotLike(String f) or findByFirstNameIsNotLike(String f)

Query based on input being a regex, with a MongoDB $not applied

findByFirstnameContaining(String f)

For a string input, query just like Like; for a collection, query testing membership in the collection

findByFirstnameNotContaining(String f)

For a string input, query like like NotLike; for a collection, query testing lack of membership in the collection

findByFirstnameRegex(String pattern)

Query using pattern as a regular expression

findByLocationNear(Point p)

Query by geospatial relation using MongoDB's $near

findByLocationNear(Point p, Distance max)

Query by geospatial relation using MongoDB's $near and $maxDistance

findByLocationNear(Point p, Distance min, Distance max)

Query by geospatial relation using MongoDB's $near, $minDistance, and $maxDistance

findByLocationWithin(Circle c)

Query by geospatial relation using MongoDB's $geoWithin, $circle, and distance

findByLocationWithin(Box b)

Query by geospatial relation using MongoDB's $geoWithin, $box, and square coordinates

findByActiveIsTrue()

Query by active being true

findByActiveIsFalse()

Query by active being false

findByLocationExists(boolean e)

Query by location having the same Boolean value as the input

All of these aforementioned keywords can also be used to construct deleteBy methods.

Many of these operators also work with other supported data stores including JPA, Apache Cassandra, Apache Geode, and GemFire to name a few. However, be sure to check the specific reference guide.

While the previous table shows all the keywords supported for MongoDB repository queries, the following list shows the various supported return types:

  • Image (or Java primitive types)
  • Iterable<Image>
  • Iterator<Image>
  • Collection<Image>
  • List<Image>
  • Optional<Image> (Java 8 or Guava)
  • Option<Image> (Scala or Vavr)
  • Stream<Image>
  • Future<Image>
  • CompletableFuture<Image>
  • ListenableFuture<Image>
  • @Async Future<Image>
  • @Async CompletableFuture<Image>
  • @Async ListenableFuture<Image>
  • Slice<Image>
  • Page<Image>
  • GeoResult<Image>
  • GeoResults<Image>
  • GeoPage<Image>
  • Mono<Image>
  • Flux<Image>
Spring Data blocking APIs support void return types as well. In Reactor-based programming, the equivalent is Mono<Void>, because the caller needs the ability to invoke subscribe().

In a nutshell, just about every container type is covered by Spring Data, which means that we can pick the right solution to suit our needs. Since this book's focus is reactive programming, we'll stick with Mono and Flux, considering they encapsulate asynchronous + non-blocking + lazy, without impacting the client, and regardless of quantity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.23