More examples with play and reactive mongo v.0.9

Showing 1-4 of 4 messages
More examples with play and reactive mongo v.0.9 jakob dobrzynski 4/30/13 3:06 AM
Hello!

I have upgrade to 0.9 and I see that there have been a lot of changes.

I have a controller where I'm calling my mongo instance to get some leadCategories:

val db = ReactiveMongoPlugin.db
  lazy val collection = db("leadCategories")

  def list(id: Long) = Action {
    implicit request => {
      Async {
        implicit val reader = LeadCategory.LeadCategoryReader
        val query = if (id > 0) BSONDocument("caregoryId" -> BSONLong(id)) else BSONDocument()
        val found = collection.find(query)
        found.toList.map {
          lc =>
            Ok(views.html.list(lc))
        }
      }
    }

This doesn't work for several reasons:

1. Handlers
import reactivemongo.bson.handlers.DefaultBSONHandlers.DefaultBSONDocumentWriter
import reactivemongo.bson.handlers.DefaultBSONHandlers.DefaultBSONReaderHandler
Seems to be gone 

2. Plugin
val db = ReactiveMongoPlugin.db
lazy val collection = db("leadCategories")
No longer works as before.

with makes:
3. 
Async {
        implicit val reader = LeadCategory.LeadCategoryReader
        val query = if (id > 0) BSONDocument("caregoryId" -> BSONLong(id)) else BSONDocument()
        val found = collection.find(query)
        found.toList.map {
          lc =>
            Ok(views.html.list(lc))
        }
      }
Not work =)

I looked at the provided example and it looks nice but pretty basic. I have also read the docs for the changes
but I can't figure it out how to change my controller to work with my current case classes. Maybe
I need to change them too? So my question is how should i implement the controller so that it works with 0.9.
Maybe you have some more cool examples?

Kind regards,

Jakob

CASE CLASSES BELOW:


package models

import org.joda.time.DateTime
import reactivemongo.bson._


import reactivemongo.bson.BSONLong

case class LeadCategory(
                         id: Option[BSONObjectID],
                         categoryId: Long,
                         categoryName: String,
                         creationDate: Option[DateTime],
                         updateDate: Option[DateTime],
                         words: List[Word]
                         )

case class Word(id: Option[BSONObjectID], word: String)

object LeadCategory {

  implicit object LeadCategoryWriter extends BSONDocumentWriter[LeadCategory] {
    def write(leadCategory: LeadCategory): BSONDocument = 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
    )
  }

  implicit object LeadCategoryReader extends BSONDocumentReader[LeadCategory] {
    def read(doc: BSONDocument): LeadCategory = {
      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)
    }
  }

}

object Word {

  implicit object Word extends BSONDocumentWriter[Word] {
    def write(word: Word): BSONDocument = BSONDocument(
      "_id" -> word.id.getOrElse(BSONObjectID.generate),
      "word" -> BSONString(word.word)
    )
  }

  implicit object LeadCategoryReader extends BSONDocumentReader[LeadCategory] {
    def read(doc: BSONDocument): Word = {
      Word(
        doc.getAs[BSONObjectID]("_id"),
        doc.getAs[BSONString]("word").get.value
      )
    }
  }
}












Re: More examples with play and reactive mongo v.0.9 Joern Bernhardt 4/30/13 7:47 AM
Hi,

Stephane posted some example code a while back:

https://github.com/zenexity/ReactiveMongo/blob/master/driver/samples/BSON.scala

1. Handlers: I think they are gone...?
2. db(<collectionName>) -> We do it like this now, don't know whether this is the right way to do it, but it works ;)
val driver new MongoDriver()
val connection = driver.connection(<list of hosts>)
val database = connection(<database name>)
3. Don't know anything about that, sorry.

4. Regarding your code: I posted something to your stackoverflow question here: http://stackoverflow.com/questions/16262089/how-do-i-map-my-objects-correctly-to-make-a-list-of-objects-work-in-mongo-play/16293012#16293012
I had this problem once, that a List[MyDataType] didn't work. It was the order of the implicit format objects for me, but I also import play.modules.reactivemongo.json.ImplicitBSONHandlers._ and use JsValue/JsObject stuff. I'm not sure whether this applies to your code.

HTH,
Joern
Re: More examples with play and reactive mongo v.0.9 jakob dobrzynski 4/30/13 8:27 AM
Hi there!

I looked at your example and listen to your wise words and everything works! We should probably post the correct code for others to see so
could you update your answer with the code below at stackoverlow and I will mark it as correct.

What I did was to: 
implement def write and def read 
change the order of classes
also change the controller code to work with 0.9

Controller:

package controllers

import models._
import play.api.mvc._
import play.api.Play.current
import play.modules.reactivemongo._
import reactivemongo.bson._
import reactivemongo.api.collections.default.BSONCollection
import org.joda.time.DateTime

object LeadController extends Controller with MongoController  {

  override val db = ReactiveMongoPlugin.db
  val col = db[BSONCollection]("leadCategories")

    def list(id: Long) = Action {
    implicit request => {

      Async {
        implicit val leadCategoryReader = LeadCategory.LeadCategoryReader

        val query = if (id > 0) BSONDocument("caregoryId" -> BSONLong(id)) else BSONDocument()
        val found = col.find(query).cursor[LeadCategory]

        found.toList.map {
          lc =>
            Ok(views.html.list(lc))
        }
      }
    }
  }
}

The case classes

package models

import org.joda.time.DateTime
import reactivemongo.bson._

case class Word(id: Option[BSONObjectID], text: String)

case class LeadCategory(
                         id: Option[BSONObjectID],
                         categoryId: Long,
                         categoryName: String,
                         creationDate: Option[DateTime],
                         updateDate: Option[DateTime],
                         words: List[Word]
                         )


object Word {

  implicit object WordWriter extends BSONDocumentWriter[Word] {
    def write(word: Word): BSONDocument = BSONDocument(
      "_id" -> word.id.getOrElse(BSONObjectID.generate),
      "text" -> BSONString(word.text)
    )
  }

  implicit object WordReader extends BSONDocumentReader[Word] {
    def read(doc: BSONDocument): Word = {
      Word(
        doc.getAs[BSONObjectID]("_id"),
        doc.getAs[BSONString]("text").get.value
      )
    }
  }
}

object LeadCategory {


  implicit object LeadCategoryWriter extends BSONDocumentWriter[LeadCategory] {
    def write(leadCategory: LeadCategory): BSONDocument = 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
    )
  }

  implicit object LeadCategoryReader extends BSONDocumentReader[LeadCategory] {
    def read(doc: BSONDocument): LeadCategory = {
      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
      )
    }
  }

}

Regards

Jakob









Re: More examples with play and reactive mongo v.0.9 Joern Bernhardt 5/1/13 12:33 AM
Glad that it helped. Just put a link as a comment to my answer.