不将Scala的None持久化,而是持久化为null值

4
我注意到scala驱动程序(版本1.2.1)将None的Option值写成null,以对应字段。在这种情况下,我更愿意完全省略该字段。这可能吗? 示例
case class Test(foo: Option[String])
persist(Test(None))

导致
> db.test.find()
{ "_id": "...", "foo": null }

但我希望能够实现

> db.test.find()
{ "_id": "..." }

当我使用Casbah时,我认为我的预期行为是默认的。

persist方法是做什么的?将case class转换为文档?这就是您应该过滤不想持久化的值的地方。 - Ross
2个回答

3

1

我在mongodb的bug跟踪器中提出了一个功能请求(https://jira.mongodb.org/browse/SCALA-294),得到了Ross Lawley的回答。他建议将转换代码(从case类转换为文档)更改为

def toDocument(t: Test) = Document("foo" -> t.foo)

将其翻译为中文:

变成类似于


def toDocument(t: Test) = {
  val d = Document()
  t.foo.foreach{ value =>
    d + ("foo" -> value)
  }
  d
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接