2013-03-03 45 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

相關問題