crockpotveggies
crockpotveggies

Reputation: 13310

ReactiveMongo query failing to paginate

Having some issues getting ReactiveMongo 0.11 to paginate my query. The behavior is that it is returning all results, instead of the page-by-page results.

Here's my query:

def listConvos(userId: String, page: Int, pageSize: Int) = {
val query = BSONDocument("userId" -> userId)
val sort = BSONDocument("lastActivity" -> -1)
val skipN = (page-1) * pageSize
val queryOptions = new QueryOpts(skipN = skipN, batchSizeN = pageSize, flagsN = 0)

collection.find(query).options(queryOptions).sort(sort).cursor[ConvoDesc](ReadPreference.primaryPreferred).collect[List]()

Note that pagination starts at 1. Using the IntelliJ debugger, the values for the variables above are:

userId = "29aosidfj43903p"
query = BSONDocument(<non-empty>)
sort = BSONDocument(<non-empty>)
queryOptions = QueryOpts(10,10,0)
page = 2
pageSize = 10

I can also confirm I've set a compound index on the userId and lastActivity fields. Thanks for your help!

Upvotes: 0

Views: 919

Answers (1)

crockpotveggies
crockpotveggies

Reputation: 13310

The solution was to also include the page size in the collect method like so:

collection.find(query).options(queryOptions).sort(sort).cursor[ConvoDesc](ReadPreference.primaryPreferred).collect[List](pageSize)

Upvotes: 3

Related Questions