简体   繁体   English

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

[英]How do I map my objects correctly to make a list of objects work in mongo + play2

I'm trying to write readers/writers for my case classes after reading: 我在阅读后试图为我的案例类编写读者/作者:

but I'm having trouble in making it work. 但是我很难让它发挥作用。

I have a leadCategory that can consist of several word objects. 我有一个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: 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)
    }
  }

Word: 字:

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

  }    
}

I'm getting a compile error at: 我收到编译错误:

"words" -> leadCategory.words)

stating: 说明:

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

What have I missed? 我错过了什么? Maybe I have misunderstood the docs... 也许我误解了文档......

All of your types should a derivative of a BSON type. 所有类型都应该是BSON类型的衍生物。 I think you want to use BSONArray instead of a List. 我想你想使用BSONArray而不是List。

Try to declare the "Word" implicit objects above the "LeadCategory", if you are putting all of it in the same file. 如果要将所有内容放在同一个文件中,请尝试在“LeadCategory”上面声明“Word”隐式对象。

Scala cannot find the implicit objects for the List[Word] - assuming you are using ReactiveMongo 0.9. Scala无法找到List [Word]的隐含对象 - 假设您使用的是ReactiveMongo 0.9。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用shell正确查询我的mongo数据库? - How do i correctly query my mongo db using shell? 如何在play2应用程序中构造嵌套的reactmongo调用? - How should I structure my nested reactivemongo calls in my play2 application? 如何将 map 对象数组转换为 MongoDB 上的现有数组 - How do I map array of objects to an existing array on MongoDB 如何将 map 对象数组转换为 Spring Data Mongo 中的普通值数组 - How to map array of objects to array of plain values in Spring Data Mongo 我如何映射对象的嵌套数组,其中包含对象的嵌套数组在react,redux中 - how do I map over a nested array of objects which contains a nested array of objects in react, redux 我应该如何在play2和Scala中处理过滤器和期货 - How should I handle Filter and Futures in play2 and Scala 如何实现Mongo DB查询以查找数组内所有对象的键? - How do I implement Mongo DB query to find key in all objects inside an array? 如何模拟MongoDB对象来测试我的数据模型? - How do I mock MongoDB objects to test my data models? 如何将json对象列表转换为javascript对象列表? - How do you convert a list of json objects to a list of javascript objects? 如何根据 url 查询字符串中的对象在 mongo 中排序? - How do sort in mongo based on objects in url query string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM