Lift-mongo-record:在mongo集合中清空JsonObjectField

时间:2011-06-17 16:06:33

标签: json scala mongodb playframework lift

我正在尝试使用JsonObjectField保存记录(使用lift-mongo- 在Play框架中记录)但在数据库集合中它是空的。 那是我的代码:

定义类:

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]

我尝试以这种方式保存记录:

Wish.createRecord.body("body").tags(List("tags", "test")).form(new Criterion("From", "Arbat")).save

在mongodb收藏中,我有点像:

{ "_id" : ObjectId("4dfb6f45e4b0ad4484d3e8c6"), "body" : "body", "tags" : [ "tags", "test" ], "form" : { } }

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的代码看起来很好。也许尝试使用MongoCaseClassField,如:


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

以下是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