Search code examples
scalacasbah

How to convert MongoDBList to BasicDBList?


I use Casbah 2.5.0. There is example in tutorial:

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"]

So newLst here is BasicDBList.

But when I try it myself, it works different.

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 here is of type MongoDBList.

Why is so? How can I convert MongoDBList to BasicDBList?


Solution

  • There is an implicit conversion from com.mongodb.casbah.commons.MongoDBList to com.mongodb.BasicDBList in casbah (check com.mongodb.casbah.commons.Implicits):

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

    Just pass MongoDBList to position where BasicDBList is expected:

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

    If you want to create 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"]