Class PagingQuery


  • 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

      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 of this 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
    • Constructor Detail

      • PagingQuery

        public PagingQuery​(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate)
        Construct an instance.
        Parameters:
        mongoTemplate - the MongoTemplate that this instance will use internally
    • Method Detail

      • with

        public PagingQuery with​(org.springframework.data.domain.Pageable pageable)
        Add/change the pagination of this instance.
        Overrides:
        with in class org.springframework.data.mongodb.core.query.Query
        Parameters:
        pageable - the Pageable 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 on Pageable specified in the with(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 of AggregationOperation will actually work here, which is why it is marked as beta. For example, using an Aggregation.project(String...) will fail because the internal implementation is assuming the "shape" of the result is an AggregateResult with the given type T and it performs its own internal projection and faceting. Other types may work, for example Aggregation.match(Criteria) works, as do lookup (i.e. "left join") aggregations.

        Note also that a sort must be specified in the Pageable given to with(Pageable). Otherwise an UncategorizedMongoDbException 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 collection
        aggregationOps - one or more aggregation operations
        Returns:
        the aggregated Page