【发布时间】:2016-03-07 09:47:54
【问题描述】:
我是 Reactivemongo 数据库 (2.6) 的新手,因为我正在尝试从我的本地系统使用 Play Framework/Scala 存储在 Mongodb 中(尝试使用 Play 2.2.3 和 2.3.8)。我试过了:
import play.api.libs.json.{JsObject, JsValue, OWrites}
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection._
import scala.concurrent.ExecutionContext.Implicits.global
object Application extends Controller with MongoController {
def jsinfocollection: JSONCollection = db.collection[JSONCollection]("mycollection")
implicit val jsvalueWrites = new OWrites[JsValue] {
println("implicit val jsvalueWrites ...")//getting the message on Play console
override def writes(o: JsValue): JsObject = o match {
case o : JsObject => o
case _ => throw new IllegalArgumentException("Only JsObjects can be stored")
}
}
def createJson = Action.async(parse.json) {
println("createJson calling...")//getting the message on Play console
request =>
jsinfocollection.insert(request.body).map{
println("jsinfocollection.insert...")//getting the message on Play console
r => Created(s"JsonInfo is Saved with result $r")
}
}
}
我已经在 Mongodb 中创建了一个集合,例如:>db.createCollection("mycollection")
{ "ok" : 1 }
但如果我给:>db.mycollection.count() - 给 0,否则如果我给:db.mycollection.find() - 什么也不给
请告诉我如何将我的 json 数据插入到我需要的集合中("mycollection")。提前致谢。
我在控制台上得到以下信息:
implicit val jsvalueWrites ...
createJson calling...
jsinfocollection.insert...
[error] play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: Only JsObjects can be stored
Internal server error, for (POST) [/createJson]
【问题讨论】:
-
您使用的是相当旧的版本,用于 Play 和 ReactiveMongo。
-
@cchantep,我可以知道任何解决方案,因为我正在使用 Play(2.2.3) 和 Mongo(2.6)。
-
我怀疑你是否会通过弃用的版本获得很多社区支持
-
@cchantep,好的,我可以在新版本中了解它吗?
-
您的错误与 Mongo 无关。请阅读有关操作的 Play 文档。
标签: mongodb scala playframework-2.3 reactivemongo play-reactivemongo