How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration mongodb mongodb

How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration


Answer coming from : http://grokbase.com/t/gg/rogue-users/1367nscf80/how-to-update-a-record-with-mongocaseclassfield-when-case-class-contains-a-scala-enumeration#20130612woc3x7utvaoacu7tv7lzn4sr2q

But more convenient directly here on StackOverFlow:


Sorry, I should have chimed in here sooner.

One of the long-standing problems with Rogue was that it was too easy toaccidentally make a field that was not serializable as BSON, and have itfail at runtime (when you try to add that value to a DBObject) rather thanat compile time.

I introduced the BSONType type class to try to address this. The upside isit catches BSON errors at compile time. The downside is you need to make achoice when it comes to case classes.

If you want to do this the "correct" way, define your case class plus aBSONType "witness" for that case class. To define a BSONType witness, youneed to provide serialization from that type to a BSON type. Example:

 case class TestCC(v: Int) implicit object TestCCIsBSONType extends BSONType[TestCC] {   override def asBSONObject(v: TestCC): AnyRef = {     // Create a BSON object     val ret = new BasicBSONObject     // Serialize all the fields of the case class     ret.put("v", v.v)     ret   } }

That said, this can be quite burdensome if you're doing it for each caseclass. Your second option is to define a generic witness that works for anycase class, if you have a generic serialization scheme:

 implicit def CaseClassesAreBSONTypes[CC <: CaseClass]: BSONType[CC] =new BSONType[CC] {   override def asBSONObject(v: CC): AnyRef = {     // your generic serialization code here, maybe involving formats   } }

Hope this helps,