Your SlideShare is downloading. ×
0
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Talk about ReactiveMongo at MSUG May
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Talk about ReactiveMongo at MSUG May

386

Published on

My talk about ReactiveMongo driver at Moscow Scala user group May meetup

My talk about ReactiveMongo driver at Moscow Scala user group May meetup

Published in: Software
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
386
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Reactive Mongo Asynchronous driver for MongoDB
  • 2. About me • My name is Andrey Neverov • “General rhetoric” • scala since 2011 • now: scala, akka, spray, mongodb • contribute to reactive mongo
  • 3. Plan 1. What is reactive mongo 2. Connecting 3. Queries 4. Working with data 5. Streaming 6. Reactive Mongo + Play
  • 4. Reactive Mongo • Asynchronous driver • Non-blocking I/O • Scalability • Streaming • GridFS
  • 5. val driver = new MongoDriver val connection = driver. connection(List(“localhost")) val db = connection.db("db_name") val collection = db.collection(“coll_name") Connecting
  • 6. Queries val query = BSONDocument( “age” -> BSONDocument( “$gt” -> 5 ) )
  • 7. Queries val animalsOlderThanFive = collection. find(query). cursor[BSONDocument]. collect[List]() ! // Future[List[BSONDocument]]
  • 8. Queries animalsOlderThanFive.map { case animals: List[Animal] => … } recover { case t: Throwable => // fail! }
  • 9. Readers case class Animal(_id: BSONObjectID, age: Int) object Animal { implicit val reader = new BSONDocumentReader[Animal] { def read(doc: BSONDocument) = { Animal(…) }}}
  • 10. Readers object Animal { import reactivemongo.bson.Macros ! val handler = Macros.handler[Animal] }
  • 11. Readers val animalsFuture = collection. find(query). cursor[Animal]. collect[List]() !
  • 12. Streaming // Enumerator[BSONDocument] val enumeratorOfAnimals = collection. find(query). cursor[BSONDocument]. enumerate()
  • 13. Streaming // Iteratee[BSONDocument, Unit] val process: = Iteratee.foreach { a => val name = a[String]("name") val pretty = BSONDocument.pretty(a) println(s"got $name of: $pretty") }
  • 14. Streaming enumeratorOfAnimals.apply(process) ! or: ! enumeratorOfAnimals |>>> process
  • 15. Play • Play json <-> Reactive mongo bson • Async actions • Iteratees • GridFS
  • 16. Play object Application extends Controller with MongoController { ! def collection: JSONCollection = db.collection(“animals") }
  • 17. Play object JsonFormats { import play.api.libs.json.Json import play.api.data._ import play.api.data.Forms._ ! implicit val format = Json.format[Animal] }
  • 18. Play def findByName(name: String) = Action.async { collection. find(Json.obj("name" -> name)) cursor[JsObject]. collect[List]() map { case animals => Ok(Json.arr(animals)) }}
  • 19. Play def fromJson = Action.async(parse.json) { r => r.body.validate[Animal].map { a => collection.insert(a).map { … } }.getOrElse { Future(BadRequest(“bad json")) }}
  • 20. Play def upload = Action(gridFSBodyParser(gfs)) { r => // Future[ReadFile[BSONValue]] val futureFile = r.body.files.head.ref futureFile.map { file => // do something Ok } }
  • 21. Thanks! • http://www.mongodb.org/ • http://reactivemongo.org/ • https://github.com/ReactiveMongo/ReactiveMongo/ • https://github.com/neverov

×