3

I connect to MongoDB with Scala using :

val driver = new MongoDriver
val connection = driver.connection(List("myhost"))
val db = connection.database("mydb")

This works fine but how to integrate this with a Play controller :

@Singleton
class ReactiveController @Inject() (implicit system: ActorSystem, materializer: Materializer, val reactiveMongoApi: ReactiveMongoApi)
    extends Controller with MongoController with ReactiveMongoComponents {

Do I need to inject a custom ReactiveMongoApi with my DB configuration ?

Or do I need to modify application.conf with my DB settings ?

I'm using play 2.5 and http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html provides this code :

package api

import reactivemongo.api.{ DB, MongoConnection, MongoDriver }

trait ReactiveMongoApi {
  def driver: MongoDriver
  def connection: MongoConnection
  def db: DB
}

But I'm unsure how to integrate it with my Play application ?

I think I'm not aware of some standard method of configuring DB sources with a Play! application ?

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • If you read the doc you've linked in the question, you can see the section "Configure your database access" which indicate you need to add the `mongodb.uri` setting in the `application.conf` – cchantep May 25 '16 at 22:49
  • 1
    and add play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule" – andrey.ladniy May 26 '16 at 03:56
  • 1
    If you want a working example of Play 2.5 with latest MongoDB driver as a reference then check out https://www.lightbend.com/activator/template/play-reactive-mongo-db – jacks Oct 15 '16 at 18:36

1 Answers1

5

Make sure you have correct configs in application.conf

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"
mongodb.uri = "mongodb://localhost:27017/demodb"

You need to inject and change mongo code as below

class MongoUserDao @Inject() (val reactiveMongoApi: ReactiveMongoApi)
extends UserDao {
//  val users = reactiveMongoApi.db.collection[JSONCollection]("users") -- old API
//   def find(userId:UUID):Future[Option[User]] =
//    users.find(Json.obj("id" -> userId)).one[User]  -- old code

  def usersF = reactiveMongoApi.database.map(_.collection[JSONCollection]("users"))  //new API

  def find(userId:UUID):Future[Option[User]] = for {
    users <- usersF
    user <- users.find(Json.obj("id" -> userId)).one[User]
  } yield user     // new code
}

If you compare new api code with old api code, reactiveMongoApi.database.map returns Future[Collection].

Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49
Pari
  • 1,443
  • 3
  • 19
  • 34
  • I do not understand how this works. ReactiveMongoApi, f I understood well, has not a property named "database", and if I write "db" I can not call "map" after. Should I import "reactiveMongoApi" from another place? @Pariksheet_Barapatre thank you for your answer – agusgambina Nov 19 '16 at 16:45
  • 1
    These are new APIs which are introduced in 0.11 release. Make sure, you use "org.reactivemongo" %% "play2-reactivemongo" % "0.11.14", import statement would be - import play.modules.reactivemongo.ReactiveMongoApi – Pari Nov 22 '16 at 06:12