2

I have class MyClass with Option field (e.g. f:Option[String]). I update it by the value (e.g. Some("abc")) and it warks. If I change it on another value (e.g. Some("xyz")) it works too. But I cannot update it on None now. There is no error. Simply value is not changed and I face old one. My update method

def update(id: String, document: T)(implicit writer: Writes[T]): Future[Either[ServiceException, T]] = {
document.update = Some(new DateTime())
Logger.debug(s"Updating document: [collection=$collectionName, id=$id, document=$document]")
Recover(collection.update(DBQueryBuilder.id(id), DBQueryBuilder.set(document))) {
  document
}

}

in DBQueryBuilder:

def id(objectId: String): JsObject = id(BSONObjectID(objectId))
def id(objectId: BSONObjectID): JsObject = Json.obj(jsId -> objectId)
def set[T](data: T)(implicit writer: Writes[T]): JsObject = Json.obj(SET -> data)

and

implicit val myClassDbFormat: Format[MyClass] = Json.format[MyClass]

I can see proper field values in logs but if value = None => nothing I use play2-reactivemongo 0.10.5.0.akka23. Scala.

Thanks in advance

Alex
  • 87
  • 2
  • 11

1 Answers1

0

i just wonder there is a problem with Json.format[MyClass] for Option.

Personally, I often define readers and writers for all models.

This is a simple example. I hope it helps you.

https://github.com/luongbalinh/play-mongo/blob/master/app/models/User.scala

Luong Ba Linh
  • 802
  • 5
  • 20