mongodb - Misunderstanding error about ReactiveMongo -


i defined following class want modelize :

case class record(                      recordkey: string,                      channels: map[string, channel],                      )    object record {     implicit val recordformat = json.format[record]   } 

now, want 1 object of type reactive mongo (in class):

import scala.concurrent.duration._ import scala.concurrent.{future, await} import scala.concurrent.executioncontext.implicits.global import reactivemongo.api._ import reactivemongo.api.collections.bson.bsoncollection import reactivemongo.bson.bsondocument  object test {   val collection = connect()   val timeout = 10.seconds   def connect() : bsoncollection = {     val config = configfactory.load()     val driver = new mongodriver     val connection = driver.connection(list(config.getstring("mongodb.uri")))     val db = connection("/toto")     db.collection("foo")   }    def findrecord(recordkey : string) : record = {     return test.collection       .find(bsondocument("recordkey"->recordkey))       .one[record]   } 

but code doesn't compile :

 not find implicit value parameter reader: reactivemongo.bson.bsondocumentreader[record] 

could explain how fix issue ?

i test :

def findrecord(recordkey : string) : record = {     val futurerecord : future[option[record]] =       test.collection         .find(bsondocument("recordkey"->recordkey))         .one[record]     return await.result(futurerecord, 10.seconds).getorelse(null)   } 

i added build.sbt :

librarydependencies ++= seq(   "org.apache.spark" % "spark-streaming_2.10" % "1.5.2",   "org.apache.spark" % "spark-streaming-kafka_2.10" % "1.5.2",   "org.slf4j" % "slf4j-api" % "1.7.13",   "org.slf4j" % "slf4j-simple" % "1.7.13",   "com.amazonaws" % "aws-java-sdk" % "1.10.12",   "com.typesafe.play" % "play-json_2.10" % "2.4.6",   "com.typesafe" % "config" % "1.3.0",   "org.scalaj" %% "scalaj-http" % "2.2.1",   "com.typesafe.akka" % "akka-actor_2.10" % "2.3.14",   "org.reactivemongo" %% "reactivemongo" % "0.11.9",   "com.github.nscala-time" %% "nscala-time" % "2.6.0" ) 

note not play app.

you need define bsondocumentreader case class record. here link documentation. similar play json readers , writers reactive mongo needs understand how convert , forth between domain object , bsondocument. similar play json can write these out in more manual style(write bsondocumentreader instance , bsondocumentwriter instance) , customize every detail , apply transformations etc. similar in style play json's format used above reactivemongo provide helpful macros generate these classes you.

for record class need add implicit val object:

import reactivemongo.bson._  implicit val recordhandler: bsonhandler[bsondocument, record] = macros.handler[record]  /* or 1 of these [if ever writing or reading data etc:  implicit val recordreader: bsondocumentreader[record] = macros.reader[record]  implicit val recordwriter: bsondocumentwriter[record] = macros.writer[record] */ 

i try start macros , see if meet needs. if need more control of processing/transformation can define own bsondocumentreader , bsondocumentwriter instances.

updated record class

import play.api.libs.json.json import reactivemongo.bson._   case class channel(label: string,amplitude: double,position: option[string])  object channel {   implicit val channelformat = json.format[channel]   implicit val channelhandler: bsonhandler[bsondocument, channel] =   macros.handler[channel]  }  object recordtype extends enumeration {  type recordtype = value  val t1 = value  implicit val enumformat = new format[recordtype] {   def reads(json: jsvalue) = jssuccess(recordtype.withname(json.as[string]))   def writes(enum: recordtype) = jsstring(enum.tostring)  }  implicit  object recordtypereader extends bsondocumentreader[recordtype] {   def read(doc: bsondocument) : recordtype  = {      recordtype.withname(doc.getas[string]("recordtype").get)   } } implicit object recordtypewriter extends bsondocumentwriter[recordtype] {   def write(recordtype: recordtype) : bsondocument = bsondocument(     "recordtype" -> bsonstring(recordtype.tostring)   )  } }  case class record(recordkey: string,recordtype: recordtype.value,channels: map[string, channel])  object record {  implicit val recordformat = json.format[record]  implicit val recordhandler: bsonhandler[bsondocument, record] =  macros.handler[record] } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -