2013-04-28 14 views
3

工作,我想寫我的情況班讀/寫器:如何將我的對象正確讀取後映射,使對象的列表中蒙戈+ play2

但我在使它工作時遇到了麻煩。

我有一個leadCategory可以由幾個單詞對象組成。

package models 

import org.joda.time.DateTime 
import reactivemongo.bson._ 
import reactivemongo.bson.handlers.{BSONWriter, BSONReader} 
import reactivemongo.bson.BSONDateTime 
import reactivemongo.bson.BSONString 

LeadCategory:

case class LeadCategory(
         id: Option[BSONObjectID], 
         categoryId: Long, 
         categoryName: String, 
         creationDate: Option[DateTime], 
         updateDate: Option[DateTime], 
         words: List[Word] 
         ) 

object LeadCategory { 

    implicit object LeadCategoryBSONReader extends BSONReader[LeadCategory] { 
    def fromBSON(document: BSONDocument): LeadCategory = { 
     val doc = document.toTraversable 
     LeadCategory(
     doc.getAs[BSONObjectID]("_id"), 
     doc.getAs[BSONLong]("caregoryId").get.value, 
     doc.getAs[BSONString]("categoryName").get.value, 
     doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)), 
     doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)), 
     doc.getAs[List[Word]]("words").toList.flatten) 
    } 
    } 

    implicit object LeadCategoryBSONWriter extends BSONWriter[LeadCategory] { 
    def toBSON(leadCategory: LeadCategory) = { 
     BSONDocument(
     "_id" -> leadCategory.id.getOrElse(BSONObjectID.generate), 
     "caregoryId" -> BSONLong(leadCategory.categoryId), 
     "categoryName" -> BSONString(leadCategory.categoryName), 
     "creationDate" -> leadCategory.creationDate.map(date => BSONDateTime(date.getMillis)), 
     "updateDate" -> leadCategory.updateDate.map(date => BSONDateTime(date.getMillis)), 
     "words" -> leadCategory.words) 
    } 
    } 

字:

"words" -> leadCategory.words) 

case class Word(id: Option[BSONObjectID], word: String) 

object Word { 

    implicit object WordBSONReader extends BSONReader[Word] { 
    def fromBSON(document: BSONDocument): Word = { 
     val doc = document.toTraversable 
     Word(
     doc.getAs[BSONObjectID]("_id"), 
     doc.getAs[BSONString]("word").get.value 
    ) 
    } 
    } 

    implicit object WordBSONWriter extends BSONWriter[Word] { 
    def toBSON(word: Word) = { 
     BSONDocument(
     "_id" -> word.id.getOrElse(BSONObjectID.generate), 
     "word" -> BSONString(word.word) 
    ) 
    } 

    }  
} 

我在得到一個編譯錯誤10

陳述:

Too many arguments for method apply(ChannelBuffer) 
Type mismatch found: (String, List[Words]) required required implicits.Producer[(String, BsonValue)] 

我錯過了什麼?也許我誤解了文檔...

+0

感謝您的提示,但我不確定自己理解。因此,而不是「單詞」 - > leadCategory.words我應該把它包裝在一個BsonValue中?你能舉個例子嗎? – jakob 2013-04-28 10:55:10

回答

0

嘗試聲明「LeadCategory」上方的「Word」隱式對象,如果您將它全部放在同一個文件中。

Scala找不到列表的隱含對象[Word] - 假設您使用的是ReactiveMongo 0.9。

+0

查看[郵件列表上的問題](https://groups.google.com/forum/?fromgroups=#!topic/reactivemongo/jvDE9KF9kdM)獲取最終代碼。 – Narigo 2013-05-01 07:31:32

0

所有類型都應該是BSON類型的派生類型。我想你想使用BSONArray而不是List。

相關問題