2013-03-03 69 views
0

我使用Casbah 2.5.0。有教程中的例子:如何将MongoDBList转换为BasicDBList?

scala> val builder = MongoDBList.newBuilder 
scala> builder += "foo" 
scala> builder += "bar" 
scala> builder += "x" 
scala> builder += "y" 
builder.type = [email protected] 

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

有从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 from 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"] 
+0

谢谢。还有一个问题。有一种简单的方法将List转换为MongoDBList(或BasicDBList)? – un1t 2013-03-03 17:04:16

+1

我更新了我的答案 – 2013-03-03 19:15:03

+0

@SergeyPassichenko可以,请回答http://stackoverflow.com/questions/15188605/scala-casbah-how-to-convert-list-to-mongodblist以及? – 2013-03-03 19:25:26

相关问题