PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/casbah/BeerDemo.scala

http://github.com/bfritz/mongodb-from-scala
Scala | 57 lines | 37 code | 14 blank | 6 comment | 0 complexity | 7f6dacb64b00198869cda9739af540e5 MD5 | raw file
  1. package casbah
  2. import com.novus.casbah.mongodb.Imports._
  3. class BeerDemo() {
  4. val conn = MongoConnection("localhost", 27017) // default host and port
  5. val fridge = conn("indyscala")("beer")
  6. // we need some beer; let's start with Casbah's MongoDBObject...
  7. val guinness: DBObject = MongoDBObject(
  8. "name" -> "Guinness Draught",
  9. "variety" -> "stout",
  10. "origin" -> "Dublin, Ireland",
  11. "attributes" -> List("creamy", "widget can"))
  12. // ...more beer via Scala 2.8 builder
  13. val builder =MongoDBObject.newBuilder
  14. builder += "name" -> "Bully Porter"
  15. builder += "variety" -> "porter"
  16. builder += "origin" -> "Kansas City, Missouri, US"
  17. builder += "abv" -> .052
  18. builder += "attributes" -> List("craft beer")
  19. val bullyPorter = builder.result.asDBObject
  20. // save our beer for consumption later
  21. fridge += guinness
  22. fridge += bullyPorter
  23. // let's add a beer to pick on...
  24. val keystoneLight: DBObject = MongoDBObject(
  25. "name" -> "Keystone Light",
  26. "variety" -> "lager",
  27. "origin" -> "Golden, Colorado, US",
  28. "abv" -> .042,
  29. "attributes" -> List("tastes like water"))
  30. fridge += keystoneLight
  31. println("Beer stored safely.")
  32. // now where did I put that beer!?
  33. fridge.find().foreach { b =>
  34. println("Let's drink a %s!".format(b("name")))
  35. }
  36. // let's find a good US beer
  37. val query = (
  38. "attributes" $ne "tastes like water",
  39. "origin" -> ", US$".r)
  40. val goodBeer = fridge.findOne(query).foreach { b =>
  41. println("\n%s is a good beer.".format(b("name")))
  42. }
  43. }