I use playframework with "org.mongodb.scala" %% "mongo-scala-driver" % "1.0.1" and have that code:
val collection = Mongo.db.getCollection("regionAuth")
def getRegions = {
val find: Observable[Document] = collection.find()
Logger.info("regions searching")
find.subscribe(new Observer[Document] {
override def onError(e: Throwable): Unit = Logger.error("regions error", e)
override def onSubscribe(subscription: Subscription): Unit = Logger.info("subscribed")
override def onComplete(): Unit = Logger.info("regions done")
override def onNext(result: Document): Unit = Logger.info("region accepted")
})
find.map { region =>
Logger.info("region accepted by map")
region
}
find.foreach(_ => Logger.info("region accepted by foreach"))
find.toFuture().onComplete {
case Success(r) => Logger.info("regions accepted as future seq " + r.size)
case Failure(e) => Logger.error("regions error as future", e)
}
find
}
And it prints:
[info] application - regions searching
[info] application - subscribed
[info] application - region accepted by foreach
[info] application - region accepted by foreach
[info] application - region accepted by foreach
[info] application - regions accepted as future seq 3
Why subscribe's events and map do not work?