我创建了一个简单的Play-Scala应用程序来测试ReactiveMongo并遇到一个奇怪的异常。这些是步骤:
制作新的Play-scala应用
activator new test-mongo
根据此链接配置application.conf和build.sbt( http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html)
修改控制器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) } } }
[DetailedDatabaseException: DatabaseException['not authorized for query on posts.posts' (code = 13)]]
Scala: 2.11.6 Play: 2.4 Mongo: 3.0.7 ReactiveMongo: 0.11.9
答案 0 :(得分:0)
使用play2-reactivemongo 0.12.0-SNAPSHOT时问题解决了。但是,每次运行之前都必须“清理”代码。