I created a simple Play-Scala application to test ReactiveMongo and encountered a strange exception. These are the steps:
Create a new play-scala app
activator new test-mongoConfigure application.conf and build.sbt according to this link ( http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html)
Modify the controller Application.scala
package controllers import play.api._ import play.api.mvc._ import javax.inject.Inject import play.api.mvc.Controller import scala.concurrent.{ ExecutionContext, Future } import scala.concurrent.ExecutionContext.Implicits.global import play.api.Play.current import play.api.libs.json._ import reactivemongo.bson.BSONDocument import reactivemongo.api.commands.WriteResult import reactivemongo.api.Cursor import play.modules.reactivemongo._ import play.modules.reactivemongo.json._ import play.modules.reactivemongo.json.collection.JSONCollection import play.modules.reactivemongo.ReactiveMongoApi class Application @Inject() (val reactiveMongoApi: ReactiveMongoApi) extends Controller with MongoController with ReactiveMongoComponents { def collection: JSONCollection = db.collection[JSONCollection]("posts") def index = Action.async { val cursor: Cursor[JsObject] = collection. // find all people with name `name` find(Json.obj("username" -> "Rob")). // sort them by creation date sort(Json.obj("created" -> -1)). // perform the query and get a cursor of JsObject cursor[JsObject] // gather all the JsObjects in a list val futurePersonsList: Future[List[JsObject]] = cursor.collect[List]() // transform the list into a JsArray val futurePersonsJsonArray: Future[JsArray] = futurePersonsList.map { persons => Json.arr(persons) } // everything's ok! Let's reply with the array futurePersonsJsonArray.map { persons => // Ok(views.html.index("Your new application is ready." + persons)) Ok(persons) } } }
- Run the app and browse the application page. On first run, the following exception will appear. Refresh the page and the data will be displayed.
[DetailedDatabaseException: DatabaseException['not authorized for query on posts.posts' (code = 13)]]
- The following shows the versions of the software used:
Scala: 2.11.6 Play: 2.4 Mongo: 3.0.7 ReactiveMongo: 0.11.9
authMode=SCRAM-SHA1- cchantep.databaseinstead of.dbfrom the API, to have the DB resolution using network failover (in case of latency). - cchantep