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