Lift-mongo-record: Empty JsonObjectField in mongo collection
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 M开发者_运维知识库ongoRecord[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:
object form extends MongoCaseClassField[Wish, Criterion](this)
case class Criterion (key: String, value: String){}
//btw you can leave out the "new" for case classes - it calls
//the automatically generated apply method
Wish.createRecord.body("body").tags(List("tags", "test"))
.form(Criterion("From","Arbat")).save
Here's an example for MongoCaseClassListField:
object form extends MongoCaseClassListField[Wish, Criterion](this)
Wish.createRecord.body("body").tags(List("tags","test"))
.form(List(Criterion("key1","val1"), Criterion("key2", "val2"))
.save
精彩评论