Mongodb Scala Play 2.4 ReactiveMongo数据库异常
我创建了一个简单的Play Scala应用程序来测试ReactiveMongo,但遇到了一个奇怪的异常。以下是步骤:Mongodb Scala Play 2.4 ReactiveMongo数据库异常,mongodb,scala,playframework-2.0,reactivemongo,Mongodb,Scala,Playframework 2.0,Reactivemongo,我创建了一个简单的Play Scala应用程序来测试ReactiveMongo,但遇到了一个奇怪的异常。以下是步骤: 创建一个新的play scala应用程序 activator新测试mongo 根据此链接配置application.conf和build.sbt( ) 修改controller Application.scala 运行应用程序并浏览应用程序页面。第一次运行时,将出现以下异常。刷新页面,将显示数据 以下显示了所用软件的版本: 使用play2 reactivemongo 0.1
activator新测试mongo
使用play2 reactivemongo 0.12.0-SNAPSHOT时解决的问题。但是,每次运行前都必须“清理”代码。您确定应用程序中使用的用户有足够的权限阅读收集文章吗?关于mongodb引发的错误代码13,您可以参考类似的SO线程:如果您使用的是mongodb 3.x的新安装,请确保使用
authMode=SCRAM-SHA1
Yes配置连接,authMode=SCRAM-SHA1已经添加,并且已经授予了权限。奇怪的是,在启动应用程序并首次浏览应用程序之后,“未授权”异常会发生一次。在以后的访问中,这将变得正常。您可以尝试使用API中的.database
而不是.db
,以使用网络故障切换(在延迟的情况下)获得db分辨率。@cchantep,您是对的。新功能在稳定的0.11.9版本中可用
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