gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:55:53 26 4
gpt4 key购买 nike

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

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com