3

阅读后,我正在尝试为我的案例课程编写读者/作者:

但我无法让它发挥作用。

我有一个可以由几个单词对象组成的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

铅类别:

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)
    }
  }

单词:

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)
      )
    }

  }    
}

我在以下位置收到编译错误:

"words" -> leadCategory.words)

说明:

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

我错过了什么?也许我误解了文档......

4

2 回答 2

0

您的所有类型都应该是 BSON 类型的派生。我认为您想使用 BSONArray 而不是列表。

于 2013-04-30T01:18:24.640 回答
0

如果您将所有对象放在同一个文件中,请尝试在“LeadCategory”上方声明“Word”隐式对象。

Scala 找不到 List[Word] 的隐式对象 - 假设您使用的是 ReactiveMongo 0.9。

于 2013-04-30T05:48:07.390 回答