Question

I described model as case class WorkAreaType(name: String) and findAll method

object WorkAreaType {
  import system.db.Mongo.JsonFormats._
  def findAll = Await.result(Mongo.workAreaTypes.find(Json.obj()).cursor[WorkAreaType].collect[List](), 3 seconds)
}

JsonFormats:

object JsonFormats {
    implicit val workAreaTypeFormat = Json.format[WorkAreaType]
}

Mongo.workAreaTypes is a JSONCollection val workAreaTypes: JSONCollection = db.collection[JSONCollection]("WorkAreaTypes")

How can I read mongo _id object after call WorkAreaType.findAll in list?

Was it helpful?

Solution

The quick but not so great way is to define your case class as

case class WorkAreaType(_id:Option[BSONObjectID] , name: String)

The reason this is not so great is that this will make reactive mongo "leak" through your repository/dao/storage layer whatever you name it.

A "better" option would be to manipulate the json to "flatten"the object id before you read it to a scala object and inflate it after you write it back to json. This can be accomplished using Json transformers or JsZippers but takes a bit of research.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top