Search code examples
mongodbplayframeworkplayframework-2.2reactivemongo

Reactivemongo QueryOpts.batchSizeN not work


I use play Reactivemongo plugin and Reactivemongo work together.

def list(ps: Int = pageSize, page: Int = 1, sortBy: String = "_id", order: Int = 1,   filterKey: String = "", filter: String = "") = Action.async { implicit request =>
  val builder = filterKey.length > 0 && filter.length > 0 match {
    case true => collection.find(Json.obj(filterKey -> filter))
    case false => collection.genericQueryBuilder
  }

  val cursor:Cursor[JsObject] = builder
    .sort(Json.obj(sortBy -> order))
    .options(QueryOpts(skipN = (page - 1) * ps, batchSizeN = 10)).cursor[JsObject]

  val futurePersonsList = cursor.collect[List]()

  val futurePersonsJsonArray = futurePersonsList.map { list =>
    Json.arr(list)
  }

  futurePersonsJsonArray.map { list =>
  //Logger.debug(list(0).)
    Ok(list(0)).as(JSON)
 }
}

The batchSizeN of QueryOpts, I thought it would got 10 of items back but it not.
At last I change cursor.collect[List]() to cursor.collect[List](10), it works. My question is batchSizeN uses for mongodb command find().limit() same way or not?
What the difference between QueryOpts.batchSizeN and cursor.collect[List](10)?


Solution

  • Batch size is this: http://docs.mongodb.org/manual/reference/method/cursor.batchSize/

    And limit is this: http://docs.mongodb.org/manual/reference/method/cursor.limit/