Skip to content

Instantly share code, notes, and snippets.

@bhudgeons
Last active August 29, 2015 13:56
Show Gist options
  • Save bhudgeons/8873210 to your computer and use it in GitHub Desktop.
Save bhudgeons/8873210 to your computer and use it in GitHub Desktop.
scala> val test = testcollections.testcoll
test: com.mongodb.casbah.MongoCollection = testtest
scala> import com.mongodb.casbah.Imports._
import com.mongodb.casbah.Imports._
scala> test.save(MongoDBObject("list" -> List("one", "two", "three"))) // save a list to mongodb
res1: com.mongodb.WriteResult = N/A
scala> val o = test.findOne.get // get back the thing we just saved
o: test.T = { "_id" : { "$oid" : "52f559c63004ae5f4863ef4e"} , "list" : [ "one" , "two" , "three"]}
scala> o.getAs[MongoDBList]("list") // get the list out
res2: Option[com.mongodb.casbah.Imports.MongoDBList] = Some([ "one" , "two" , "three"])
scala> o.getAs[MongoDBList]("list").get.toList // turn it from Option[MongoDBList] to a scala List[Any]
res3: List[Any] = List(one, two, three)
scala> o.getAs[MongoDBList]("list").map(_.toList) // same thing, but use map so we get an Option[List[Any]]
res8: Option[List[Any]] = Some(List(one, two, three))
scala> o += "list" -> List("one", "two", "three") // set the "list" again, the same way we did before
res4: com.mongodb.casbah.commons.MongoDBObject = { "_id" : { "$oid" : "52f559c63004ae5f4863ef4e"} , "list" : [ "one" , "two" , "three"]}
scala> o.getAs[MongoDBList]("list") // get the list out again
res5: Option[com.mongodb.casbah.Imports.MongoDBList] = Some(List(one, two, three))
scala> o.getAs[MongoDBList]("list").get.toList // same thing we did before ... works fine.
res6: List[Any] = List(one, two, three)
scala> o.getAs[MongoDBList]("list").map(_.toList) // same thing we did before ... DOH!
java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to com.mongodb.casbah.commons.MongoDBList
at $anonfun$1.apply(<console>:15)
at scala.Option.map(Option.scala:145)
at .<init>(<console>:15)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577)
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833)
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:900)
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24)
at sbt.Logger$$anon$4.apply(Logger.scala:90)
at sbt.TrapExit$App.run(TrapExit.scala:244)
at java.lang.Thread.run(Thread.java:722)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment