0

我使用 Casbah 2.5.0。教程中有示例:

scala> val builder = MongoDBList.newBuilder
scala> builder += "foo"
scala> builder += "bar"
scala> builder += "x"
scala> builder += "y"
builder.type = com.mongodb.casbah.commons.MongoDBListBuilder@...

scala> val newLst = builder.result
newLst: com.mongodb.BasicDBList = [ "foo" , "bar" , "x" , "y"]

所以这里的newLst 是BasicDBList。

但是当我自己尝试时,它的工作方式不同。

scala> val builder = MongoDBList.newBuilder
scala> builder += "foo"
scala> builder += "bar"
scala> val newLst = builder.result
newLst: com.mongodb.casbah.commons.MongoDBList = [ "foo" , "bar"]

这里的 newLst 是 MongoDBList 类型。

为什么会这样?如何将 MongoDBList 转换为 BasicDBList?

4

1 回答 1

1

在 casbah 中存在从com.mongodb.casbah.commons.MongoDBListto的隐式转换(检查):com.mongodb.BasicDBListcom.mongodb.casbah.commons.Implicits

implicit def unwrapDBList(in: MongoDBList): BasicDBList = in.underlying

只需将 MongoDBList 传递到预期 BasicDBList 的位置:

scala>  val l: BasicDBList = newLst
l: com.mongodb.casbah.Imports.BasicDBList = [ "foo" , "bar"]

如果你想从MongoDBList创建List

scala>  val list = List("foo", "bar")
list: List[java.lang.String] = List(foo, bar)

scala>  val dbList = MongoDBList(list:_*)
dbList: com.mongodb.casbah.commons.MongoDBList = [ "foo" , "bar"]
于 2013-03-03T16:47:27.430 回答