PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/scala_mongodb/chicago_scala_oct10/code/casbah_iteration_example.scala

https://github.com/bwmcadams/presentations
Scala | 28 lines | 14 code | 3 blank | 11 comment | 2 complexity | 236f87c0dbf523abb0078dc67538621b MD5 | raw file
  1. val mongoColl = MongoConnection()("casbah_test")("test_data")
  2. val user1 = MongoDBObject("user" -> "bwmcadams",
  3. "email" -> "~~bmcadams~~<AT>novusDOTcom")
  4. val user2 = MongoDBObject("user" -> "someOtherUser")
  5. mongoColl += user1
  6. mongoColl += user2
  7. mongoColl.find()
  8. // com.novus.casbah.mongodb.MongoCursor =
  9. // MongoCursor{Iterator[DBObject] with 2 objects.}
  10. for { x <- mongoColl} yield x
  11. /* Iterable[com.mongodb.DBObject] = List(
  12. { "_id" : { "$oid" : "4c3e2bec521142c87cc10fff"} ,
  13. "user" : "bwmcadams" ,
  14. "email" : "~~bmcadams~~<AT>novusDOTcom"},
  15. { "_id" : { "$oid" : "4c3e2bec521142c87dc10fff"} ,
  16. "user" : "someOtherUser"}
  17. ) */
  18. mongoColl.findOne(q).foreach { x =>
  19. // do some work if you found the user...
  20. println("Found a user! %s".format(x("user")))
  21. }
  22. // Or limit the fields returned
  23. val q = MongoDBObject.empty
  24. val fields = MongoDBObject("user" -> 1)
  25. for (x <- mongoColl.find(q, fields)) println(x)