For general pagination, you can use the .skip() and .limit() in the Query object, which you can pass as arguments to your method:
Query query = new Query(); query.addCriteria(Criteria.where("a").is("b")); query.skip(10); query.limit(10); List<Foo> results = mongoOperation.find(query, Foo);
With .skip() will show how the results can go, and .skip() will show the returned page size.
Get an instance of MongoOperations from MongoTemplate and use the standard .find() operation .find() .
Skipping and limiting is not the most efficient option, but try to store the last values ββseen by the natural index, for example, _id , where possible, and use range queries to avoid skipping 1000 results.
Query query = new Query(); query.addCriteria(Criteria.where("_id").gt(lastSeen)); query.limit(10);
source share