I’m trying to save a record with JsonObjectField (using lift-mongo-
record in Play framework) but it is empty in database collection.
That’s my code:
Define classes:
class Wish extends MongoRecord[Wish] with MongoId[Wish] {
def meta = Wish
object body extends StringField(this, 1024)
object tags extends MongoListField[Wish, String](this)
object form extends JsonObjectField[Wish, Criterion](this, Criterion) {
def defaultValue = null.asInstanceOf[Criterion]
}
}
object Wish extends Wish with MongoMetaRecord[Wish] {
override def collectionName = "wishes"
}
case class Criterion (key: String, value: String) extends JsonObject[Criterion] {
def meta = Criterion
}
object Criterion extends JsonObjectMeta[Criterion]
I try to save record in that way:
Wish.createRecord.body("body").tags(List("tags", "test")).form(new Criterion("From", "Arbat")).save
And in mongodb collection I have smth like:
{ "_id" : ObjectId("4dfb6f45e4b0ad4484d3e8c6"), "body" : "body", "tags" : [ "tags", "test" ], "form" : { } }
What am I doing wrong?
Your code looks fine. Maybe try using MongoCaseClassField like:
Here’s an example for MongoCaseClassListField: