Thursday, May 9, 2013

Simple Pagination for MongoDB Queries Using QueryDSL

So, I have looked at several Java APIs for MongoDB integration; my favorite by far is Spring Data.  That said, Spring has API classes for paging and sorting.  Even with those classes, I find the QueryDSL API from mysema to be very functional.  To work with my Maven projects, I add the below plugin and dependencies:

Once my Maven configuration is set, I need to update the domain model objects that I will be using with QueryDSL.  Below is a snippet from my Employee model object with the @QueryEntity annotation added:

@Document(collection = "employees")
public class Employee extends Person {

Next I need to update the Spring Data Employee Repository interface to extend the QueryDslPredicateExecutor interface, type by my Employee model class.  At this point it is important to note that for the this integration, I had to switch from the annotated Mongo Repository definition to extending   the MongoRepository interface.  The @RepositoryDefinition annotation did not want to play nice with the QueryDslPredicateExecutor extension.

// @RepositoryDefinition(domainClass = Employee.class, idClass = String.class)
public interface EmployeeRepository extends MongoRepository<Employee, String>,
  QueryDslPredicateExecutor<Employee> {
Next, I need to update my EmployeeService interface and EmployeeServiceImpl implementation class to add a new method to access the generated find...() methods added for me by QueryDSL.

 public Page<Employee> findAllWithPages(int pageStart, int pageSize,
   Sort.Direction sortDirection, String sortField) {
  PageRequest pageRequest = new PageRequest(pageStart, pageSize,
    new Sort(Sort.Direction.ASC, "employeeId"));
  return this.employeeRepository.findAll(pageRequest);
In this new method, I build a object and a object to pass into the newly provisioned findAll(..) method on the EmployeeRepository.  I did not write this new findAll(...) method, it was generated for me by QueryDSL and Spring Data.  Additionally, QueryDSL created the QEmployee class for me and placed it into target/generated-soutces/java in my Maven project.  QEmployee is a query type and is seen in its entirety below.

import static com.mysema.query.types.PathMetadataFactory.*;

import com.mysema.query.types.*;
import com.mysema.query.types.path.*;

 * QEmployee is a Querydsl query type for Employee
public class QEmployee extends EntityPathBase<Employee> {

    private static final long serialVersionUID = -236647047;

    public static final QEmployee employee = new QEmployee("employee");

    public final QPerson _super = new QPerson(this);

    public final SimplePath<Address> address = createSimple("address", Address.class);

    public final DateTimePath<java.util.Date> birthDate = _super.birthDate;

    public final SimplePath<Department> department = createSimple("department", Department.class);

    public final StringPath employeeId = createString("employeeId");

    public final StringPath firstName = _super.firstName;

    public final DateTimePath<java.util.Date> hireDate = createDateTime("hireDate", java.util.Date.class);

    public final StringPath id =;

    public final StringPath lastName = _super.lastName;

    public final StringPath middleName = _super.middleName;

    public final NumberPath<Integer> salary = createNumber("salary", Integer.class);

    public final StringPath title = createString("title");

    public QEmployee(String variable) {
        super(Employee.class, forVariable(variable));

    public QEmployee(BeanPath<? extends Employee> entity) {
        super(entity.getType(), entity.getMetadata());

    public QEmployee(PathMetadata<?> metadata) {
        super(Employee.class, metadata);


Finally, below is the JUnit test that calls the pagination code generated for me.
package com.icfi.mongo;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;


public class PagingQueryTest {
 private static Logger log = LoggerFactory.getLogger(PagingQueryTest.class);

 private ApplicationContext ctx;
 MongoOperations mongoOps;
 List<Employee> employees;
 EmployeeService employeeService;

 public void setup() {
  ctx = new GenericXmlApplicationContext("context/main.xml");
  mongoOps = (MongoOperations) ctx.getBean("mongoTemplate");
  employeeService = (EmployeeService) ctx.getBean("employeeService");

 public void testPaging() {
  String[] lastNames = new String[] { "Stanfel", "Gustavson", "Lortz",
    "Marquardt", "Unno", "Savasere", "Spelt", "Wynblatt",
    "Danecki", "Weedman", "Hartvigsen", "Menhoudj", "Heyers",
    "Willoner", "Shumilov", "Zuberek", "Boguraev" };
  int pageCount = 10;
  int pageNumber = 0;
  String sortField = "employeeId";
  Sort.Direction sortOrder = Sort.Direction.ASC;

  Page<Employee> employeesPage = employeeService.findAllWithPages(
    pageNumber, pageCount, sortOrder, sortField);

  while (employeesPage.hasNextPage()) {

   assertEquals("List size is incorrect.", pageCount,
     employeesPage.getSize());"Page Number = " + pageNumber);

   if (employeesPage.hasContent()) {
      .get(employeesPage.getSize() - 1).getLastName());

      "Last name was incorrect.",
        .get(employeesPage.getSize() - 1).getLastName());


   employeesPage = employeeService.findAllWithPages(pageNumber,
     pageCount, sortOrder, sortField);
  }"Page Number = " + pageNumber);

  employeesPage = employeeService.findAllWithPages(pageNumber, pageCount,
    sortOrder, sortField);
    .get(employeesPage.getContent().size() - 1).getLastName());

    "Last name was incorrect.",
      .get(employeesPage.getContent().size() - 1)

 public void tearDown() {

With this approach I have quickly added pagination to my MongoDB queries, while writing minimal code.


  1. Now the fifty percent (50%) of the population around the world are using the internet. And by 2020 the active internet users are crossing 65% of the population across the world. data science course syllabus

  2. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work!
    data science course in India

  3. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    Artificial Intelligence Course

  4. Your content is very unique and understandable useful for the readers keep update more article like this.
    best data science courses

  5. I was looking at a portion of your posts on this site and I consider this site is really enlightening! Keep setting up..
    business analytics course aurangabad