如何将MongoDBList转换为BasicDBList?

时间:2013-03-03 16:39:19

标签: scala casbah

我使用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?

1 个答案:

答案 0 :(得分:1)

casbah中有com.mongodb.casbah.commons.MongoDBListcom.mongodb.BasicDBList的隐式转换(检查com.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"]