Package org.kiwiproject.spring.data
Class KiwiSpringMongoQueries
- java.lang.Object
-
- org.kiwiproject.spring.data.KiwiSpringMongoQueries
-
public class KiwiSpringMongoQueries extends Object
Static utilities for performing MongoDB queries using Spring Data.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKiwiSpringMongoQueries.PartialMatchTypeDefines whether to require a partial or exact match.
-
Constructor Summary
Constructors Constructor Description KiwiSpringMongoQueries()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddDateBounds(org.springframework.data.mongodb.core.query.Query query, String propertyName, Long startDateInclusiveMillis, Long endDateInclusiveMillis)Add date restrictions to the given property.static voidaddInCriteriaFromCsv(org.springframework.data.mongodb.core.query.Query query, String csv, String propertyName)Adds aCriteria.in(Object...)for the given property using values separated by commas incsv.static <T> voidaddInCriteriaFromCsv(org.springframework.data.mongodb.core.query.Query query, String csv, String propertyName, Function<String,T> converter)Adds aCriteria.in(Object...)for the given property using by first separating the values by comma incsv, and then applying the given function to each value.static voidaddMultiplePartialOrEqualMatchCriteria(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 voidaddPartialOrEqualMatchCriteria(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.
-
-
-
Method Detail
-
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 typeP- the pagination parameter type- Parameters:
mongoTemplate- theMongoTemplatethat is used to perform the MongoDB operationspagingParams- the parameters describing the desired paginationclazz- the domain/model class mapped to a Mongo collection- Returns:
- a
Pagecontaining 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
criteriaBuilderis aBiConsumerthat can be used to specify restriction criteria and/or to access or change the pagination parameters.- Type Parameters:
T- the result typeP- the pagination parameter type- Parameters:
mongoTemplate- theMongoTemplatethat is used to perform the MongoDB operationspagingParams- the parameters describing the desired paginationclazz- the domain/model class mapped to a Mongo collectioncriteriaBuilder- aBiConsumerthat can be used to add additional pagination and query criteria- Returns:
- a
Pagecontaining 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 criteriapropertyName- the property name, which is expected to be of typeDatestartDateInclusiveMillis- 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 criteriamatchString- the string to matchpropertyName- the property namematchType- 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 criteriamatchStrings- the strings to match, using an OR operationpropertyName- the property namematchType- the desired match type
-
addInCriteriaFromCsv
public static void addInCriteriaFromCsv(org.springframework.data.mongodb.core.query.Query query, String csv, String propertyName)Adds aCriteria.in(Object...)for the given property using values separated by commas incsv.- Parameters:
query- the MongoDB query on which to add the criteriacsv- a comma-separated list of acceptable valuespropertyName- 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 aCriteria.in(Object...)for the given property using by first separating the values by comma incsv, 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 criteriacsv- a comma-separated list of acceptable valuespropertyName- the property nameconverter- a function to convert the separated strings into a different type
-
-