PageRenderTime 212ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/scala_mongodb/chicago_scala_oct10/code/casbah_dbobject_sample1.scala

https://github.com/bwmcadams/presentations
Scala | 60 lines | 47 code | 7 blank | 6 comment | 0 complexity | 80630488387b8c4e1d05029eb12a1137 MD5 | raw file
  1. import com.novus.casbah.mongodb.Imports._ // Only import needed - mongoDB type aliases imported too
  2. val coll = MongoConnection()("test")("testData")
  3. // Map
  4. val map: DBObject = Map(
  5. "foo" -> "bar",
  6. "spam" -> "eggs",
  7. "up" -> "down",
  8. "pie" -> List(
  9. "cherry",
  10. "blueberry",
  11. "apple",
  12. "rhubarb",
  13. "3.14"
  14. )
  15. )
  16. // 'Product'
  17. val product: DBObject =
  18. ( "foo" -> "bar",
  19. "spam" -> "eggs",
  20. "up" -> "down",
  21. "pie" -> List(
  22. "cherry",
  23. "blueberry",
  24. "apple",
  25. "rhubarb",
  26. "3.14"
  27. )
  28. ).asDBObject // Explicit conversion method
  29. // "Factory" method
  30. val constructed: DBObject = MongoDBObject(
  31. "foo" -> "bar",
  32. "spam" -> "eggs",
  33. "up" -> "down",
  34. "pie" -> List(
  35. "cherry",
  36. "blueberry",
  37. "apple",
  38. "rhubarb",
  39. "3.14"
  40. )
  41. )
  42. // We showed the builder before
  43. val builder = MongoDBObject.newBuilder
  44. builder += "foo" -> "bar"
  45. builder += "spam" -> "eggs"
  46. builder += "up" -> "down"
  47. builder += "pie" -> List("cherry", "blueberry",
  48. "apple", "rhubarb", "3.14")
  49. val built: DBObject = builder.result
  50. // Also responds to the 'Map' methods...
  51. built += "x" -> "y"
  52. built.getOrElse("x", throw new Error("Can't find value for X"))
  53. /* res15: AnyRef = y */