mongodb - ReactiveMongo custom BSONDocument{ Reader, Writer } -
i'm new reactivemongo , want create custom document reader/writer 1 object i'm getting error
type mismatch; found : reactivemongo.bson.bsondocumentreader[reactivemongo.bson.bsondocument] required: reactivemongo.bson.bsondocumentreader[db.database.estudiante] note: implicit object estudiantereader not applicable here because comes after application point , lacks explicit result type error occurred in application involving default arguments.
this code , based in example found in reactivemongo's official webpage.
package db import scala.concurrent.executioncontext.implicits.global import reactivemongo.api._ import reactivemongo.bson._ object database { //driver para la conexion val driver = new mongodriver //conexion con mongodb val connection = driver.connection(list("localhost")) //conexion con la base de datos amdb val db = connection("reactive") //colección domainnames que se encuentra en la base de datos amdb val collection = db("estudiantes") def findalldomainnames() = { val query = bsondocument() collection.find(query).cursor[estudiante] //.cursor[bsondocument].collect[list]() } case class estudiante(id: option[bsonobjectid], nombre: string, apellidos: string, edad: int) object estudiante { implicit object estudiantewriter extends bsondocumentwriter[estudiante] { def write(estudiante: estudiante): bsondocument = { bsondocument( "_id" -> estudiante.id.getorelse(bsonobjectid.generate), "nombre" -> estudiante.nombre, "appellidos" -> estudiante.apellidos, "edad" -> estudiante.edad.tostring) } } implicit object estudiantereader extends bsondocumentreader[estudiante] { def read(doc: bsondocument): estudiante = { estudiante( doc.getas[bsonobjectid]("_id"), doc.getas[string]("nombre").get, doc.getas[string]("appellidos").get, doc.getas[int]("edad").get) } } } def main(args: array[string]) { println(findalldomainnames); } }
what missing/wrong?
thank in advance!
move declaration of object estudiante
above database
, add import in scope of database
:
import estudiante._
Comments
Post a Comment