mongodb - How to use ReactiveMongoDB and doc.getAs with a complex object? -


i working play framework , reactivemongodb. trying write reader , writer class called customer. being done in models.scala file in following order:

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

but getting error

[error] c:\users\xxxxx\git\oneid-scala\oneid-scala\app\models\models.scala:54:  value getas not 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 not member of reactivemongo.bson.bsondocument [error]         doc.getas[string]("postalcode").get, [error]   

for following lines

doc.getas[string]("postalcode").get,       doc.getas[string]("postalcodeextensioncode").get, 

i decided add writers in

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

and compile error changed following

[info] compiling 2 scala sources 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 not applicable here b ecause comes after application point , lacks explicit result type  [error]        doc.getas[string]("streetaddressline").get, [error]                                               ^ [error] 1 error found [error] (compile:compileincremental) compilation failed [error] total time: 4 s, completed jul 11, 2015 12:40:20 pm 

thank cchantep, following code compiles now. found there 2 parts answer here.

a. in order have embedded custom classes in class, each class defined must defined.

b. both writers , readers must have been defined classes or there compiler errors. can't defined readers without writers.

the following code compiles fine

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

Comments