Skip to content

Pagination

In many cases you want to have the possibility to paginate over the response of your query result. MongoCamp MongoDB Driver support the pagination over Aggregation and Filter Response. As well there is an comfort methode to have an foreach over the whole Response, but with pagination to have a lower memory footprint.

Aggregation Pagination

WARNING

The Pagination over an aggregation pipeline supports only the response of Document, also if you use an case class MongoDAO you will got an Document back.

scala
val pipeline = List(filterStage, sortStage)

val pagination = MongoPaginatedAggregation(PersonDAO.Raw, pipeline, allowDiskUse = true)

val page = pagination.paginate(1, 10)

Find Pagination

scala
val paginationFemale = MongoPaginatedFilter(PersonDAO, Map("gender" -> "female"), sortByKey("name"))

val pageFemale = paginationFemale.paginate(1, 10)

Foreach over Pagination result

With default row count

scala
var i = 0
pagination.foreach { element =>
  {
    logger.trace(element.toJson())
    i = i + 1
  }
}
i mustEqual 98

With specific row count

scala
paginationFemale.foreach(5) { person =>
  {
    logger.trace(person.toString)
    i = i + 1
  }
}
i mustEqual 98

Released under the Apache License 2.0.