scalareactivemongoplay-reactivemongo

Idiomatic way of getting a collection in ReactiveMongo


I'm writing a Play/Scala application using Play 2.5.4 and ReactiveMongo. Based on this example, I'm getting the collection using

class SettingStore( val mongo:ReactiveMongoApi) {
  def collection = mongo.db.collection[BSONCollection]("Settings")
  // more code...
}

However, db is now deprecated. The deprecation warning recommends I use database, but this one returns a Future so all operations have to be mapped over. Since ReactiveMongo's operations also return Futures, this seems redundant... what's to proper way of getting a collection? (or, am I missing something completely and Future[Future[Future[A]]] is the future?)


Solution

  • If you have a look at the documentation, you can see examples using the .database function, instead of the deprecated .db.

    The non-async .db has been deprecated as it didn't offer sufficient guaranty to be able to find an active connection in the MongoConnection pool.

    It was assuming at least one connection is active as soon as the pool is started, which is not always the case as checking/discovering the ReplicaSet nodes can take time, according the network speed/latency.

    The same assertion can be wrong in case the driver cannot join the nodes for some time (network interruption, nodes restarted, ...). It can take some time so the nodes indicate they are back on line.

    The new .database resolution is async, and use a FailoverStrategy to wait (or not) for an available connection (according the chosen read preference, ...).