Update array item in JSONCollection

79 views
Skip to first unread message

Alex Fruzenshtein

unread,
Jan 15, 2015, 8:50:25 AM1/15/15
to reacti...@googlegroups.com
Hey everyone. I'm working with Play and reactiveMongo. My app has data model such as:

case class Person(_id: Option[BSONObjectID],
                  email: String,
                  password: String,
                  education: Option[Education])

object Lawyer {

  implicit val accountWrites: Writes[Person] = (
    (JsPath \ "_id").writeNullable[BSONObjectID] and
    (JsPath \ "email").write[String] and
    (JsPath \ "password").write[String] and
    (JsPath \ "education").writeNullable[Education]
  )(unlift(Person.unapply))

  implicit val accountReads: Reads[Person] = (
    (JsPath \ "_id").readNullable[BSONObjectID].map(_.getOrElse(BSONObjectID.generate)).map(Some(_)) and
    (JsPath \ "email").read[String] and
    (JsPath \ "password").read[String] and
    (JsPath \ "education").readNullable[Education]
  )(Person.apply _)

case class Education(status: String, certificates: Option[Seq[Certificate]])

object Education {

  implicit val educationFormat: Format[Education] = (
    (JsPath \ "status").format[String] and
    (JsPath \ "certificates").formatNullable[Seq[Certificate]]
  )(Education.apply, unlift(Education.unapply))

}

case class Certificate(id: Option[String] = Some(Random.alphanumeric.take(12).mkString),
                       name: String,
                       licenseCode: Option[String],
                       link: Option[String],
                       date: Date)

object Certificate {

  implicit val certificateFormat = Json.format[Certificate]

}


I'm interested in function which will update an item in Seq[Certificate]. The one thing I've developed - rewrite entire Seq, when I need to update even single item in it.

How can I do this more optimal?

Thanks

David Bouyssié

unread,
Jan 30, 2015, 5:05:50 AM1/30/15
to reacti...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages