Skip to content

Instantly share code, notes, and snippets.

@adrianojlt
Created September 23, 2025 14:31
Show Gist options
  • Select an option

  • Save adrianojlt/8ea3986a6ada98073e7b5fb5ad71633e to your computer and use it in GitHub Desktop.

Select an option

Save adrianojlt/8ea3986a6ada98073e7b5fb5ad71633e to your computer and use it in GitHub Desktop.
mongoTemplate with pagination
Pageable pageable = PageRequest.of(0, 10);
// Build query without pagination first
Query query = new Query();
// Add criteria's here
long total = mongoTemplate.count(query, Patient.class);
query.with(pageable); // Apply pagination only for the find operation
List<Patient> filteredPatients = mongoTemplate.find(query, Patient.class, "patient");
Page<Patient> patientPage = new PageImpl<>(filteredPatients, pageable, total);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment