0
votes

I created a simple Play-Scala application to test ReactiveMongo and encountered a strange exception. These are the steps:

  1. Create a new play-scala app

    activator new test-mongo

  2. Configure application.conf and build.sbt according to this link ( http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html)

  3. 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)
    }
  }

}
  1. 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)]]
  1. The following shows the versions of the software used:
Scala: 2.11.6 
Play: 2.4 
Mongo: 3.0.7
ReactiveMongo: 0.11.9
1
Are you sure that the user you are using in your application has enough rights to read the collection posts ? You can refer to similar SO thread about error code 13 raised by mongodb : stackoverflow.com/questions/23619018/… - Louis F.
And if you're using a fresh install of MongoDB 3.x, make sure to configure the connection with authMode=SCRAM-SHA1 - cchantep
Yes, authMode=SCRAM-SHA1 has been added and rights have been granted. It is strange that the "not authorized" exception happens once right after start up the app and browse the app for the first time. It becomes normal on subsequent visits. - Joseph Hui
You can try to use .database instead of .db from the API, to have the DB resolution using network failover (in case of latency). - cchantep
@cchantep, you are right. - Joseph Hui

1 Answers

0
votes

Problem solved when using play2-reactivemongo 0.12.0-SNAPSHOT. However, the code has to be "cleaned" every time before run.