Package org.kiwiproject.spring.data
Class PagingQuery
java.lang.Object
org.springframework.data.mongodb.core.query.Query
org.kiwiproject.spring.data.PagingQuery
- All Implemented Interfaces:
org.springframework.data.mongodb.core.ReadConcernAware,org.springframework.data.mongodb.core.ReadPreferenceAware
public class PagingQuery
extends org.springframework.data.mongodb.core.query.Query
A subclass of
Query that adds pagination helpers.
Expects a Pageable to be set when performing query operations. Use with(Pageable) to add or change
the pagination of this instance.
-
Constructor Summary
ConstructorsConstructorDescriptionPagingQuery(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate) Construct an instance. -
Method Summary
Modifier and TypeMethodDescription<T> org.springframework.data.domain.Page<T>aggregatePage(Class<T> clazz, org.springframework.data.mongodb.core.aggregation.AggregationOperation... aggregationOps) Aggregates a page of results for the given type of object, which is assumed to be mapped to a MongoDB collection.<T> org.springframework.data.domain.Page<T>Finds a specific page for the given type of object, which is assumed to be mapped to a MongoDB collection.with(org.springframework.data.domain.Pageable pageable) Add/change the pagination ofthisinstance.Methods inherited from class org.springframework.data.mongodb.core.query.Query
addCriteria, allowDiskUse, allowSecondaryReads, collation, comment, cursorBatchSize, equals, exhaust, fields, getCollation, getCriteria, getFieldsObject, getHint, getKeyset, getLimit, getMeta, getQueryObject, getReadConcern, getReadPreference, getRestrictedTypes, getSkip, getSortObject, hashCode, hasKeyset, hasReadConcern, hasReadPreference, isLimited, isRestrictedTypeKey, isSorted, limit, limit, maxTime, maxTimeMsec, noCursorTimeout, of, partialResults, query, querySettingsEquals, restrict, setMeta, skip, toString, with, with, with, with, withHint, withHint, withReadConcern, withReadPreference
-
Constructor Details
-
PagingQuery
public PagingQuery(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate) Construct an instance.- Parameters:
mongoTemplate- theMongoTemplatethat this instance will use internally
-
-
Method Details
-
with
Add/change the pagination ofthisinstance.- Overrides:
within classorg.springframework.data.mongodb.core.query.Query- Parameters:
pageable- thePageablethat specifies this query's pagination- Returns:
- this instance
-
findPage
Finds a specific page for the given type of object, which is assumed to be mapped to a MongoDB collection.- Type Parameters:
T- the result type- Parameters:
clazz- the domain/model class mapped to a Mongo collection- Returns:
- a
Pageof results - Implementation Note:
- Due to DATAMONGO-1783, we have to create
a
Querywith the same criteria but which is not paginated; otherwise the count query is limited to the limit specified onPageablespecified in thewith(Pageable)method. We don't quite understand why you would ever want to limit a count query, especially in the context of pagination, since you basically can get an incorrect result that is always limited to the specified count.
-
aggregatePage
@Beta public <T> org.springframework.data.domain.Page<T> aggregatePage(Class<T> clazz, org.springframework.data.mongodb.core.aggregation.AggregationOperation... aggregationOps) Aggregates a page of results for the given type of object, which is assumed to be mapped to a MongoDB collection. Please make sure to read the caveats and possible problems sections below.Caveats and Possible Problems
Not all types ofAggregationOperationwill actually work here, which is why it is marked as beta. For example, using anAggregation.project(String...)will fail because the internal implementation is assuming the "shape" of the result is anAggregateResultwith the given typeTand it performs its own internal projection and faceting. Other types may work, for exampleAggregation.match(Criteria)works, as do lookup (i.e. "left join") aggregations.Note also that a sort must be specified in the
Pageablegiven towith(Pageable). Otherwise, anUncategorizedMongoDbExceptionwill be thrown with the error: "$sort stage must have at least one sort key".Recommendations for New Code
Based on the above restrictions and potential usage problems, we strongly recommend avoiding this method for new code, as its original purpose was very limited in scope, mainly to perform lookup (join) operations in order to return an "aggregate" page that contained model objects as well as their associated objects. Unfortunately, since we have various usages of this method sprinkled across a few dozen Dropwizard services, we cannot remove it until we find and replace all those usages (with something else that we have not implemented as of now).- Type Parameters:
T- the result type- Parameters:
clazz- the domain/model class mapped to a Mongo collectionaggregationOps- one or more aggregation operations- Returns:
- the aggregated
Page
-