在scala play和reactiveMongodb中不推荐使用Symbol JSONCollection

时间:2016-07-08 13:03:05

标签: mongodb scala playframework-2.0

name := """sample"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.8"

resolvers ++= Seq(
  "Atlassian Releases" at "https://maven.atlassian.com/public/",
  "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases",
  Resolver.sonatypeRepo("snapshots")
 )

pipelineStages := Seq(digest,gzip)

libraryDependencies ++= Seq(
  cache,
  filters,
  "com.typesafe.play" %% "play-mailer" % "3.0.1",
  "com.mohiva" %% "play-silhouette" % "3.0.0",
  "org.webjars" %% "webjars-play" % "2.4.0",
  "net.codingwell" %% "scala-guice" % "4.0.0",
  "net.ceedubs" %% "ficus" % "1.1.2",
  "com.adrianhurt" %% "play-bootstrap3" % "0.4.4-P24",
  "com.mohiva" %% "play-silhouette-testkit" % "3.0.0" % "test",
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.14-play24",
  "org.reactivemongo" %% "reactivemongo-play-json" % "0.11.14",
  specs2 % Test
)

routesGenerator := InjectedRoutesGenerator

scalacOptions ++= Seq(
  "-encoding", "UTF-8",
  "-deprecation",
  "-feature",
  "-unchecked",
  "-Xfatal-warnings",
  "-Xlint",
  "-Ywarn-adapted-args",
  "-Ywarn-dead-code",
  "-Ywarn-inaccessible",
  "-Ywarn-nullary-override",
  "-Ywarn-value-discard",
  "-language:reflectiveCalls"
)

控制器

package controllers

import javax.inject.{Inject, Singleton}

import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.json.collection.JSONCollection
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.api.commands.bson.BSONCountCommand.{ Count, CountResult }
import reactivemongo.api.commands.bson.BSONCountCommandImplicits._
import reactivemongo.bson.BSONDocument

import scala.concurrent.Future

@Singleton
class Dummy @Inject()(val reactiveMongoApi: ReactiveMongoApi) extends Controller
  with MongoController with ReactiveMongoComponents {

  def jsonCollection = reactiveMongoApi.database.collection[JSONCollection]("deals");
  def bsonCollection = reactiveMongoApi.database.collection[BSONCollection]("deals");

  //initiate the sample data for deal
  def index = Action {
    Logger.info("Application startup...")

    val posts = List(
      Json.obj(
        "name" -> "ABC",
        "description" -> "Alphabet",
        "author" -> "Steve"
      ),


    )

    val query = BSONDocument("name" -> BSONDocument("$exists" -> true))
    val command = Count(query)
    val result: Future[CountResult] = bsonCollection.runCommand(command)

    result.map { res =>
      val numberOfDocs: Int = res.value
      if(numberOfDocs < 1) {
        jsonCollection.bulkInsert(posts.toStream, ordered = true).foreach(i => Logger.info("Record added."))
      }
    }

    Ok("Your database is ready.")
  }
  // use for cleaning dummy data
  def cleanup = Action {
    jsonCollection.drop().onComplete {
      case _ => Logger.info("Database collection dropped")
    }
    Ok("Your database is clean.")
  }
}

我收到错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

看起来JSONCollection可以在两个不同的地方找到。

http://reactivemongo.org/releases/0.11/play-api/index.html#play.modules.reactivemongo.json.collection.JSONCollection http://reactivemongo.org/releases/0.11/json-api/index.html#reactivemongo.play.json.collection.JSONCollection

确保您从右侧dep(我认为是 "org.reactivemongo" %% "reactivemongo-play-json" % "0.11.14")

导入