score:0

Accepted answer

You could try to use an implicit value class to enrich the MongoCursor:

object ImplicitClassContainer {
  implicit class RichMongoCursor(val mc: MongoCursor) extends AnyVal {
    def sort(sortFun: DBObject): MongoCursor = mc.sort(sortFun)

    def limit(number: Int): MongoCursor = mc.limit(number)

    def offset(number: Int): MongoCursor = mc.skip(number)
  }

}

to be used like this

  import ImplicitClassContainer._

  def x(mc: MongoCursor): MongoCursor = {
    mc.offset(25).limit(25)
  }

Basically it's the same pattern which is used to add functionality to Array: You get additional functions in "flow style", but it's still a MongoCursor. As it is an implicit value class, no additional instance of an object is created at run-time.


Related Query

More Query from same tag