mongodb - 如何正确映射我的对象以使对象列表在 mongo + play2 中工作

标签 mongodb scala playframework playframework-2.1

我正在尝试在阅读后为我的案例类写读者/作者:

但我在让它工作时遇到了麻烦。

我有一个 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)]

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

最佳答案

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

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

关于mongodb - 如何正确映射我的对象以使对象列表在 mongo + play2 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16262089/

相关文章:

mongodb - ModuleNotFoundError : Module not found: Error: Can't resolve 'dns' in 'node_modules/mongodb/lib'

python - 如何使用 PyMongo 在 MongoDB 中创建新数据库?

scala - Scala中隐式参数的通用容器的隐式转换

scala - 为什么 2.10 坚持指定类型参数边界(在 2.9 中工作正常)?

javascript - Play Framework : How to implement REST API for File Upload

javascript - MongoDB : how to put conditional for multiple optional value

MongoDb 连接被拒绝

scala - 在 Scala 中转换日期格式

java - Play Framework 1.2.7 将表单值传递给 Controller

java - 用javascript语言编译JasperReport时出现异常