2016-10-22 10 views
0

最近スカラーを学び始め、akka HTTPとreactivemongoを使って簡単なAPIを作成しようとしました。 簡単な操作で問題があります。ドック、公式のチュートリアル、stackoverflowなどの時間を掘るのに多くの時間を費やすだろう。おそらく私は何かが非常に簡単に行方不明です。ReactMongoでCRUD操作を行う

マイコード:

object MongoDB { 
    val config = ConfigFactory.load() 
    val database = config.getString("mongodb.database") 
    val servers = config.getStringList("mongodb.servers").asScala 
    val credentials = Lis(Authenticate(database,config.getString("mongodb.userName"), config.getString("mongodb.password"))) 
    val driver = new MongoDriver 
    val connection = driver.connection(servers, authentications = credentials) 
    //val db = connection.database(database) 
} 

今、私は基本的なCRUD操作をしたいと思います。私は別のコードスニペットをしようとしていますが、それを動作させることはできません。私は、コレクションに対する操作を行うことはできません

object TweetManager { 
    import MongoDB._ 
    //taken from docs 
    val collection = connection.database("test"). 
     map(_.collection("tweets")) 
    val document1 = BSONDocument(
     "author" -> "Tester", 
     "body" -> "test" 
    ) 

    //taken from reactivemongo tutorial, it had extra parameter as BSONCollection, but can't get find the way of getting it 
    def insertDoc1(doc: BSONDocument): Future[Unit] = { 
     //another try of getting the collection 
     //def collection = for (db1 <- db) yield db1.collection[BSONCollection]("tweets") 
     val writeRes: Future[WriteResult] = collection.insert(doc) 
     writeRes.onComplete { // Dummy callbacks 
      case Failure(e) => e.printStackTrace() 
      case Success(writeResult) => 
       println(s"successfully inserted document with result: $writeResult") 
     } 
     writeRes.map(_ => {}) // in this example, do nothing with the success 
    } 
} 
insertDoc1(document1) 

は、ここではいくつかの例です。 IDEは私に "シンボルを解決できません"と言っています。コンパイラはエラーを返します:

value insert is not a member of scala.concurrent.Future[reactivemongo.api.collections.bson.BSONCollection] 

正しい方法はありますか?

答えて

0

あなたは(Future[T]ではなくT上の操作を呼び出すとReactiveMongoに特有の問題ではありません)Future[Collection]ではなく、基になるコレクションにinsert操作を呼び出そうとしています。

documentationをご覧になることをおすすめします。

関連する問題