Class KiwiSpringMongoQueries

java.lang.Object
org.kiwiproject.spring.data.KiwiSpringMongoQueries

public final class KiwiSpringMongoQueries extends Object
Static utilities for performing MongoDB queries using Spring Data.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Defines whether to require a partial or exact match.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addDateBounds(org.springframework.data.mongodb.core.query.Query query, String propertyName, @Nullable Long startDateInclusiveMillis, @Nullable Long endDateInclusiveMillis)
    Add date restrictions to the given property.
    static void
    addInCriteriaFromCsv(org.springframework.data.mongodb.core.query.Query query, String csv, String propertyName)
    Adds a Criteria.in(Object...) for the given property using values separated by commas in csv.
    static <T> void
    addInCriteriaFromCsv(org.springframework.data.mongodb.core.query.Query query, String csv, String propertyName, Function<String,T> converter)
    Adds a Criteria.in(Object...) for the given property using by first separating the values by comma in csv, and then applying the given function to each value.
    static void
    addMultiplePartialOrEqualMatchCriteria(org.springframework.data.mongodb.core.query.Query query, Collection<String> matchStrings, String propertyName, KiwiSpringMongoQueries.PartialMatchType matchType)
    Add a partial or equal match criteria for the given property and match strings.
    static void
    addPartialOrEqualMatchCriteria(org.springframework.data.mongodb.core.query.Query query, String matchString, String propertyName, KiwiSpringMongoQueries.PartialMatchType matchType)
    Add a partial or equal match criteria for the given property and match string.
    static <T, P extends PagingParams>
    org.springframework.data.domain.Page<T>
    paginate(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate, P pagingParams, Class<T> clazz)
    Paginate objects of the given class, which are assumed to be mapped to a Mongo collection, using the given paging parameters.
    static <T, P extends PagingParams>
    org.springframework.data.domain.Page<T>
    paginate(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate, P pagingParams, Class<T> clazz, BiConsumer<PagingQuery,P> criteriaBuilder)
    Paginate objects of the given class, which are assumed to be mapped to a Mongo collection, using the given paging parameters.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • paginate

      public static <T, P extends PagingParams> org.springframework.data.domain.Page<T> paginate(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate, P pagingParams, Class<T> clazz)
      Paginate objects of the given class, which are assumed to be mapped to a Mongo collection, using the given paging parameters.
      Type Parameters:
      T - the result type
      P - the pagination parameter type
      Parameters:
      mongoTemplate - the MongoTemplate that is used to perform the MongoDB operations
      pagingParams - the parameters describing the desired pagination
      clazz - the domain/model class mapped to a Mongo collection
      Returns:
      a Page containing the paginated results
    • paginate

      public static <T, P extends PagingParams> org.springframework.data.domain.Page<T> paginate(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate, P pagingParams, Class<T> clazz, BiConsumer<PagingQuery,P> criteriaBuilder)
      Paginate objects of the given class, which are assumed to be mapped to a Mongo collection, using the given paging parameters.

      The criteriaBuilder is a BiConsumer that can be used to specify restriction criteria and/or to access or change the pagination parameters.

      Type Parameters:
      T - the result type
      P - the pagination parameter type
      Parameters:
      mongoTemplate - the MongoTemplate that is used to perform the MongoDB operations
      pagingParams - the parameters describing the desired pagination
      clazz - the domain/model class mapped to a Mongo collection
      criteriaBuilder - a BiConsumer that can be used to add additional pagination and query criteria
      Returns:
      a Page containing the paginated results, optionally filtered by criteria
    • addDateBounds

      public static void addDateBounds(org.springframework.data.mongodb.core.query.Query query, String propertyName, @Nullable Long startDateInclusiveMillis, @Nullable Long endDateInclusiveMillis)
      Add date restrictions to the given property.

      Specify both start and end milliseconds (since the epoch) to create a closed range, or specify only start or end milliseconds to create an open-range. E.g. if only start milliseconds is specified, then the criteria includes only dates that are equal to or after the given value, with no upper bound.

      If both start and end milliseconds are null, the call is a no-op.

      Parameters:
      query - the MongoDB query on which to add the criteria
      propertyName - the property name, which is expected to be of type Date
      startDateInclusiveMillis - the start date, inclusive. May be null.
      endDateInclusiveMillis - the end date, inclusive. May be null.
    • addPartialOrEqualMatchCriteria

      public static void addPartialOrEqualMatchCriteria(org.springframework.data.mongodb.core.query.Query query, String matchString, String propertyName, KiwiSpringMongoQueries.PartialMatchType matchType)
      Add a partial or equal match criteria for the given property and match string.
      Parameters:
      query - the MongoDB query on which to add the criteria
      matchString - the string to match
      propertyName - the property name
      matchType - the desired match type
    • addMultiplePartialOrEqualMatchCriteria

      public static void addMultiplePartialOrEqualMatchCriteria(org.springframework.data.mongodb.core.query.Query query, Collection<String> matchStrings, String propertyName, KiwiSpringMongoQueries.PartialMatchType matchType)
      Add a partial or equal match criteria for the given property and match strings. Any of the match strings are considered to be a match, i.e. this effectively performs an OR operation.
      Parameters:
      query - the MongoDB query on which to add the criteria
      matchStrings - the strings to match, using an OR operation
      propertyName - the property name
      matchType - the desired match type
    • addInCriteriaFromCsv

      public static void addInCriteriaFromCsv(org.springframework.data.mongodb.core.query.Query query, String csv, String propertyName)
      Adds a Criteria.in(Object...) for the given property using values separated by commas in csv.
      Parameters:
      query - the MongoDB query on which to add the criteria
      csv - a comma-separated list of acceptable values
      propertyName - the property name
    • addInCriteriaFromCsv

      public static <T> void addInCriteriaFromCsv(org.springframework.data.mongodb.core.query.Query query, String csv, String propertyName, Function<String,T> converter)
      Adds a Criteria.in(Object...) for the given property using by first separating the values by comma in csv, and then applying the given function to each value.
      Type Parameters:
      T - the result type
      Parameters:
      query - the MongoDB query on which to add the criteria
      csv - a comma-separated list of acceptable values
      propertyName - the property name
      converter - a function to convert the separated strings into a different type