json - 提升-mongo-记录 : Empty JsonObjectField in mongo collection

标签 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" : { } }

我做错了什么?

最佳答案

您的代码看起来不错。也许尝试像这样使用 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


关于json - 提升-mongo-记录 : Empty JsonObjectField in mongo collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6388532/

相关文章:

python - 如何从 Python 中的 API 响应解析 JSON 数据?

java - REST - 定义来自 Restful Java 服务的 JSON 数组的名称

scala - Spark – 连接第一行匹配条件

用于范围查询的 MongoDB 索引和 ShardKey?

json - 养蜂场:是否可以记录什么是JSON响应字段?

json - 在 Ext.data 的上下文中,JsonStore 和 JsonReader 之间的基本区别是什么?

mongodb - 用于 maxdistance 和 MongoDB 的单位?

c# - c# mongodb 文档是否应该继承自 BsonDocument

orm - 使用 Squeryl 持久化集合

scala - 如何将字符串写入 Scala Process?