Reputation: 7141
I am trying to implement Pagination with Reactive Mongo.
def get(page: Int, pageSize: Int, filter: JsObject = Json.obj()): Future[Seq[Thing]] = {
val actualPage = if(page > 1) page else 1
collection
.find(filter)
.options(QueryOpts(skipN = (actualPage - 1) * pageSize, batchSizeN = pageSize))
.cursor[Thing]()
.collect[Seq](pageSize, Cursor.FailOnError[Seq[Thing]]())
}
This works and returns me a sequence of documents filtering by the page
and pageSize
. The problem is I have no idea how many results match the specified query, so I cant tell how many page
's exist.
Is there a way I can also get a $count
as part of the same query? Or do I need to resort to querying the database a second time with the below?
collection.count(filter)
Ideally I would change my return type to Future[Page[Seq[Thing]]]
where Page
would contain the total result count. e.g.
case class Page[T](results: Seq[T], count: Int)
Upvotes: 3
Views: 528