带有反应性mongo和Scala的嵌套文档

时间:2013-11-29 21:52:32

标签: mongodb scala nested document reactivemongo

我正在尝试通过Scala在MongoDB中存储嵌套文档。该文件如下:

Project {
    "_id": ObjectId("528547370cf6e41449003512"),
    "highLevelCode": NumberLong(3),
    "description": [
        {"_id": ObjectId("528547370cf6e41449003521"),
        "lang": "en",
        "desc": "desc in English"}, 
        {"_id ": ObjectId("528547370cf6e41449003522"), 
        "lang": "fr", 
        "desc": "desc en francais"}],
    "budget": NumberLong(12345)
}

基本上我想存储嵌套描述,这些描述可能是Project文档中的多种语言。

我写的代码是:

import reactivemongo.bson._
import reactivemongo.bson.handlers.{BSONWriter, BSONReader}
import reactivemongo.bson.BSONLong
import reactivemongo.bson.BSONString

case class LocaleText(
  id: Option[BSONObjectID],
  lang: String,
  textDesc: String
)

object LocaleText {
  implicit object LocaleTextBSONReader extends BSONReader[LocaleText] {
    def fromBSON(document: BSONDocument): LocaleText = {
      val doc = document.toTraversable

      LocaleText(
        doc.getAs[BSONObjectID]("_id"),
        doc.getAs[BSONString]("lang").map(_.value).get,
        doc.getAs[BSONString]("textDesc").map(_.value).get
      )
    }
  }

  implicit object LocaleTextBSONWriter extends BSONWriter[LocaleText] {
    def toBSON(localText: LocaleText) = {
      BSONDocument(
        "_id" -> localText.id.getOrElse(BSONObjectID.generate),
        "lang" -> BSONString(localText.lang),
        "textDesc" -> BSONString(localText.textDesc)
      )
    }
  }    
}

case class Project(
  id:                Option[BSONObjectID],
  description:          List[LocaleText],
  budget:               Option[Long]
  )

object Project {

  implicit object ProjectReader extends BSONReader[Project]{
    def fromBSON(doc: BSONDocument): Project = {
      val document = doc.toTraversable

      Project(
        document.getAs[BSONObjectID]("_id"),
        document.getAs[BSONArray]("description").map { values =>
            values.values.toList.flatMap { case value =>
              value match {
                case v: LocaleText => Some(v.asInstanceOf[LocaleText])
                case _ => None
              }
            }
        }.getOrElse(List.empty),
        document.getAs[BSONLong]("budget").map(_.value)
      )
    }
  }

  implicit object ProjectWriter extends BSONWriter[Project]{
    def toBSON(project: Project): BSONDocument = {
      BSONDocument(
        "_id"                   -> project.id.getOrElse(BSONObjectID.generate),
    "description"           -> BSONArray(project.description)
      ).append(Seq(
        project.budget.map(b => "budget" -> BSONLong(b))
      ).flatten:_*)
    }
  }
}

然而,它给了我编译错误,如

重载方法值适用于替代方法:[error](producer:reactivemongo.bson.Implicits.Producer [(String,reactivemongo.bson.BSONValue)],producer:reactivemongo.bson.Implicits.Producer [(String,reactivemongo。 bson.BSONValue)] )reactivemongo.bson.AppendableBSONDocument [错误](els:(String,reactivemongo.bson.BSONValue))reactivemongo.bson.AppendableBSONDocument [error]无法应用于((String,reactivemongo.bson.BSONObjectID),List [LocaleText])...

基本上Scala不喜欢这条线     “description” - > BSONArray(project.description)

但是,以下替代方法有效但我不能使用List / Array来允许两种以上的语言:

case class LocaleText(
  enDesc: String,
  frDesc: String)

case class Project(
  id:                   Option[BSONObjectID],
  description:          LocaleText)

object Project {
implicit object LocaleTextBSONReader extends BSONReader[LocaleText] {
    def fromBSON(document: BSONDocument): LocaleText = {
      val doc = document.toTraversable

      LocaleText(
        doc.getAs[BSONString]("enDesc").map(_.value).get,
        doc.getAs[BSONString]("frDesc").map(_.value).get
      )
    }
  }

  implicit object LocaleTextBSONWriter extends BSONWriter[LocaleText] {
    def toBSON(localText: LocaleText) = {
      BSONDocument(
        "enDesc" -> BSONString(localText.enDesc),
        "frDesc" -> BSONString(localText.frDesc)
      )
    }
  }    

implicit object ProjectReader extends BSONReader[Project]{
def fromBSON(doc: BSONDocument): Project = {
  val document = doc.toTraversable

  Project(
    document.getAs[BSONObjectID]("_id"),
    document.getAs[BSONString]("iatiId").map(_.value).get,
    LocaleTextBSONReader.fromBSON(document.getAs[BSONDocument]("description").get)
  }
}

implicit object ProjectWriter extends BSONWriter[Project]{
def toBSON(project: Project): BSONDocument = {
  BSONDocument(
    "_id"                   -> project.id.getOrElse(BSONObjectID.generate),
    "iatiId"                -> BSONString(project.iatiId),
    "description"           -> LocaleTextBSONWriter.toBSON(project.description) 
 }
}

如何将project.description转换为Mongo的LocaleText列表到BSONArray?如果你能解释我的问题,我感激不尽。非常感谢你的帮助。

2 个答案:

答案 0 :(得分:3)

最后,我找到了自己的问题的解决方案,希望这将有助于其他一些与ReactiveMongo 0.8斗争的人:

case class LocaleText(
    lang: String,
    desc: String)

case class Project(
    id:                 Option[BSONObjectID],
    descriptions:         List[LocaleText])

object Project {
  implicit object LocaleTextBSONReader extends BSONReader[LocaleText] {
        def fromBSON(document: BSONDocument): LocaleText = {
          val doc = document.toTraversable

          LocaleText(
            doc.getAs[BSONString]("lang").get.value,
            doc.getAs[BSONString]("desc").get.value
          )
        }
      }

      implicit object LocaleTextBSONWriter extends BSONWriter[LocaleText] {
        def toBSON(localText: LocaleText) = {
          BSONDocument(
            "lang" -> BSONString(localText.lang),
            "desc" -> BSONString(localText.desc)
          )
        }
      }    

  implicit object ProjectReader extends BSONReader[Project]{
    def fromBSON(doc: BSONDocument): Project = {
      val document = doc.toTraversable

      Project(
        document.getAs[BSONObjectID]("_id"),
        document.getAs[BSONArray]("descriptions").get.toTraversable.toList.map { descText =>
            LocaleTextBSONReader.fromBSON(descText.asInstanceOf[TraversableBSONDocument]
        }
      )
    }
  }

  implicit object ProjectWriter extends BSONWriter[Project]{
    def toBSON(project: Project): BSONDocument = {
      BSONDocument(
        "_id"                   -> project.id.getOrElse(BSONObjectID.generate),
        "descriptions"          -> BSONArray(project.descriptions.map {
        description => LocaleTextBSONWriter.toBSON(description)
      }: _*)
    }
  }

答案 1 :(得分:2)

这可能是图书馆的一个问题。我使用最新版本的reactivemongo测试了你的代码,它编译得很好(我需要调整你的代码以适应BSONReaders和BSONWriters的新语法,但这不应该对错误产生任何影响)。

使用reactivemongo 0.10.0甚至可以使用新提供的宏:

import reactivemongo.bson._

case class LocaleText(id: Option[BSONObjectID], lang: String, textDesc: String)

object LocaleText {
  implicit val localeTextBSONHandler = Macros.handler[LocaleText]
}

case class Project(id: Option[BSONObjectID], description: List[LocaleText], budget: Option[Long])

object Project {
  implicit val projectBSONHandler = Macros.handler[Project]
}