Package org.kiwiproject.spring.data
Class PagingQuery
- java.lang.Object
-
- org.springframework.data.mongodb.core.query.Query
-
- org.kiwiproject.spring.data.PagingQuery
-
public class PagingQuery extends org.springframework.data.mongodb.core.query.Query
A subclass ofQuery
that adds pagination helpers.Expects a
Pageable
to be set when performing query operations. Usewith(Pageable)
to add or change the pagination of this instance.
-
-
Constructor Summary
Constructors Constructor Description PagingQuery(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate)
Construct an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <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>
findPage(Class<T> clazz)
Finds a specific page for the given type of object, which is assumed to be mapped to a MongoDB collection.PagingQuery
with(org.springframework.data.domain.Pageable pageable)
Add/change the pagination ofthis
instance.-
Methods inherited from class org.springframework.data.mongodb.core.query.Query
addCriteria, allowSecondaryReads, collation, comment, cursorBatchSize, equals, exhaust, fields, getCollation, getCriteria, getFieldsObject, getHint, getLimit, getMeta, getQueryObject, getRestrictedTypes, getSkip, getSortObject, hashCode, isRestrictedTypeKey, isSorted, limit, maxScan, maxTime, maxTime, maxTimeMsec, noCursorTimeout, of, partialResults, query, querySettingsEquals, restrict, setMeta, skip, slaveOk, toString, useSnapshot, with, withHint, withHint
-
-
-
-
Method Detail
-
with
public PagingQuery with(org.springframework.data.domain.Pageable pageable)
Add/change the pagination ofthis
instance.- Overrides:
with
in classorg.springframework.data.mongodb.core.query.Query
- Parameters:
pageable
- thePageable
that specifies this query's pagination- Returns:
- this instance
-
findPage
public <T> org.springframework.data.domain.Page<T> findPage(Class<T> clazz)
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
Page
of results - Implementation Note:
- Due to DATAMONGO-1783, we have to create
a
Query
with the same criteria but which is not paginated; otherwise the count query is limited to the limit specified onPageable
specified 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 ofAggregationOperation
will 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 anAggregateResult
with the given typeT
and 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
Pageable
given towith(Pageable)
. Otherwise anUncategorizedMongoDbException
will 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
-
-