如何将ReactiveMongoDB和doc.getAs与复杂对象一起使用?

时间:2015-07-10 23:47:11

标签: mongodb scala reactivemongo play-reactivemongo

我正在使用Play框架和ReactiveMongoDB。我正在为我的班级写一个名为Customer的读者和作家。这是在models.scala文件中按以下顺序完成的:

import reactivemongo.bson._


case class StreetAddressLine(
  id: Option[BSONObjectID],
  StreetAddressLine: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object StreetAddressLine {
  implicit object StreetAddressLineBSONReader extends BSONDocumentReader[StreetAddressLine] {
    def read(doc: BSONDocument): StreetAddressLine =
      StreetAddressLine(
        doc.getAs[BSONObjectID]("_id"), 
        doc.getAs[String]("StreetAddressLine").get, 
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }
}  



case class PrimaryAddress(
  id: Option[BSONObjectID],
  StreetAddressLine: List[StreetAddressLine],
  PrimaryTownName: String,
  CountryISOAlpha2Code: String,
  PostalCode: String,
  PostalCodeExtensionCode: String,
  TerritoryOfficialName: String,
  TerritoryAbbreviatedName: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object PrimaryAddress {
  implicit object PrimaryAddressBSONReader extends BSONDocumentReader[PrimaryAddress] {
    def read(doc: BSONDocument): PrimaryAddress =
      PrimaryAddress(
        doc.getAs[BSONObjectID]("_id"), //Mongos internal identifier
        doc.getAs[List[StreetAddressLine]]("StreetAddressLine").get, 
        doc.getAs[String]("PrimaryTownName").get,        
        doc.getAS[String]("CountryISOAlpha2Code").get, 
        doc.getAS[String]("PostalCode").get,      
        doc.getAs[String]("PostalCodeExtensionCode").get,  
        doc.getAs[String]("TerritoryOfficialName").get,     
        doc.getAs[String]("TerritoryAbbreviatedName").get,  
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
  }
}  

但我收到错误

[error] C:\Users\xxxxx\git\oneid-scala\oneid-scala\app\models\models.scala:54:
 value getAS is not a member of reactivemongo.bson.BSONDocument
[error]         doc.getAS[String]("CountryISOAlpha2Code").get,
[error]             ^
[error] C:\Users\xxxxx\git\oneid-scala\oneid-scala\app\models\models.scala:55:
 value getAS is not a member of reactivemongo.bson.BSONDocument
[error]         doc.getAS[String]("PostalCode").get,
[error]  

以下行

doc.getAS[String]("PostalCode").get,      
doc.getAs[String]("PostalCodeExtensionCode").get,

我决定在

中添加作家
package models

import org.jboss.netty.buffer._
import org.joda.time.DateTime
import play.api.data._
import play.api.data.Forms._
import play.api.data.format.Formats._
import play.api.data.validation.Constraints._

import reactivemongo.bson._





case class StreetAddressLine(
  id: Option[BSONObjectID],
  StreetAddressLine: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object StreetAddressLine {
  implicit object StreetAddressLineBSONReader extends BSONDocumentReader[StreetAddressLine] {
    def read(doc: BSONDocument): StreetAddressLine =
      StreetAddressLine(
        doc.getAs[BSONObjectID]("_id"), 
        doc.getAs[String]("StreetAddressLine").get, 
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }
 implicit object StreetAddressLineBSONWriter extends BSONDocumentWriter[StreetAddressLine] {
    def write(streetAddressLine: StreetAddressLine): BSONDocument =
      BSONDocument(
        "_id" -> streetAddressLine.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> streetAddressLine.StreetAddressLine,
        "creationDate" -> streetAddressLine.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> streetAddressLine.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 

}  



case class PrimaryAddress(
  id: Option[BSONObjectID],
  StreetAddressLine: List[StreetAddressLine],
  PrimaryTownName: String,
  CountryISOAlpha2Code: String,
  PostalCode: String,
  PostalCodeExtensionCode: String,
  TerritoryOfficialName: String,
  TerritoryAbbreviatedName: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object PrimaryAddress {

 implicit object PrimaryAddressBSONReader extends BSONDocumentReader[PrimaryAddress] {
   def read(doc: BSONDocument): PrimaryAddress =
     PrimaryAddress(
       doc.getAs[BSONObjectID]("_id"), 
       doc.getAs[String]("StreetAddressLine").get, 
       doc.getAs[String]("PrimaryTownName").get,
       doc.getAs[String]("CountryISOAlpha2Code").get, 
       doc.getAs[String]("PostalCode").get, 
       doc.getAs[String]("PostalCodeExtensionCode").get, 
       doc.getAs[String]("TerritoryOfficialName").get, 
       doc.getAs[String]("TerritoryAbbreviatedName").get, 
       doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
       doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }  


 implicit object PrimaryAddressBSONWriter extends BSONDocumentWriter[PrimaryAddress] {
    def write(primaryAddress: PrimaryAddress): BSONDocument =
      BSONDocument(
        "_id" -> primaryAddress.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> primaryAddress.StreetAddressLine,
        "PrimaryTownName" -> primaryAddress.PrimaryTownName,
        "CountryISOAlpha2Code" -> primaryAddress.CountryISOAlpha2Code,
        "PostalCode" -> primaryAddress.PostalCode,
        "TerritoryOfficialName" -> primaryAddress.TerritoryOfficialName,
        "TerritoryAbbreviatedName" -> primaryAddress.TerritoryAbbreviatedName,
        "creationDate" -> primaryAddress.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> primaryAddress.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 



}  

编译错误更改为以下

[info] Compiling 2 Scala sources to C:\Users\xxxxx\git\oneid-scala\oneid-scala
\target\scala-2.11\classes...
[error] C:\Users\xxxxx\git\oneid-scala\oneid-scala\app\models\models.scala:62:
 type mismatch;
[error]  found   : String
[error]  required: List[models.StreetAddressLine]
[error]  Note: implicit object PrimaryAddressBSONWriter is not applicable here b
ecause it comes after the application point and it lacks an explicit result type

[error]        doc.getAs[String]("StreetAddressLine").get,
[error]                                               ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed Jul 11, 2015 12:40:20 PM

1 个答案:

答案 0 :(得分:1)

谢谢cchantep,以下代码现在编译。我发现答案分为两部分。

一个。为了在另一个类中嵌入自定义类,必须定义每个定义的类。

B中。必须为所有类定义编写器和读取器,否则将出现编译器错误。没有作者,你不能只定义读者。

以下代码编译正确

package models

import org.jboss.netty.buffer._
import org.joda.time.DateTime
import play.api.data._
import play.api.data.Forms._
import play.api.data.format.Formats._
import play.api.data.validation.Constraints._

import reactivemongo.bson._





case class StreetAddressLine(
  id: Option[BSONObjectID],
  StreetAddressLine: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object StreetAddressLine {
  implicit object StreetAddressLineBSONReader extends BSONDocumentReader[StreetAddressLine] {
    def read(doc: BSONDocument): StreetAddressLine =
      StreetAddressLine(
        doc.getAs[BSONObjectID]("_id"), 
        doc.getAs[String]("StreetAddressLine").get, 
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }
 implicit object StreetAddressLineBSONWriter extends BSONDocumentWriter[StreetAddressLine] {
    def write(streetAddressLine: StreetAddressLine): BSONDocument =
      BSONDocument(
        "_id" -> streetAddressLine.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> streetAddressLine.StreetAddressLine,
        "creationDate" -> streetAddressLine.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> streetAddressLine.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 

}  



case class PrimaryAddress(
  id: Option[BSONObjectID],
  StreetAddressLine: List[StreetAddressLine],
  PrimaryTownName: String,
  CountryISOAlpha2Code: String,
  PostalCode: String,
  PostalCodeExtensionCode: String,
  TerritoryOfficialName: String,
  TerritoryAbbreviatedName: String,
  creationDate: Option[DateTime],
  updateDate: Option[DateTime])

object PrimaryAddress {

 implicit object PrimaryAddressBSONReader extends BSONDocumentReader[PrimaryAddress] {
   def read(doc: BSONDocument): PrimaryAddress =
     PrimaryAddress(
       doc.getAs[BSONObjectID]("_id"), 
       doc.getAs[List[StreetAddressLine]]("StreetAddressLine").get, 
       doc.getAs[String]("PrimaryTownName").get,
       doc.getAs[String]("CountryISOAlpha2Code").get, 
       doc.getAs[String]("PostalCode").get, 
       doc.getAs[String]("PostalCodeExtensionCode").get, 
       doc.getAs[String]("TerritoryOfficialName").get, 
       doc.getAs[String]("TerritoryAbbreviatedName").get, 
       doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
       doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
 }  



 implicit object PrimaryAddressBSONWriter extends BSONDocumentWriter[PrimaryAddress] {
    def write(primaryAddress: PrimaryAddress): BSONDocument =
      BSONDocument(
        "_id" -> primaryAddress.id.getOrElse(BSONObjectID.generate),
        "StreetAddressLine" -> primaryAddress.StreetAddressLine,
        "PrimaryTownName" -> primaryAddress.PrimaryTownName,
        "CountryISOAlpha2Code" -> primaryAddress.CountryISOAlpha2Code,
        "PostalCode" -> primaryAddress.PostalCode,
        "TerritoryOfficialName" -> primaryAddress.TerritoryOfficialName,
        "TerritoryAbbreviatedName" -> primaryAddress.TerritoryAbbreviatedName,
        "creationDate" -> primaryAddress.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> primaryAddress.updateDate.map(date => BSONDateTime(date.getMillis)))
  } 



}