Reactive Mongo
Asynchronous driver for MongoDB
About me
• My name is Andrey Neverov
• “General rhetoric”
• scala since 2011
• now: scala, akka, spray, mongodb
• contribu...
Plan
1. What is reactive mongo
2. Connecting
3. Queries
4. Working with data
5. Streaming
6. Reactive Mongo + Play
Reactive Mongo
• Asynchronous driver
• Non-blocking I/O
• Scalability
• Streaming
• GridFS
val driver = new MongoDriver	
val connection = driver.	
connection(List(“localhost"))	
val db = connection.db("db_name")	
...
Queries
val query = BSONDocument(	
“age” -> BSONDocument(	
“$gt” -> 5	
)	
)
Queries
val animalsOlderThanFive = 	
collection.	
find(query).	
cursor[BSONDocument].	
collect[List]()	
!
// Future[List[B...
Queries
animalsOlderThanFive.map { 	
case animals: List[Animal] => …	
} recover {	
case t: Throwable => // fail!	
}
Readers
case class Animal(_id: BSONObjectID, 	
	 	 	 	 	 	 	 	 age: Int)	
object Animal {	
implicit val reader = 	
new BSO...
Readers
object Animal {	
import reactivemongo.bson.Macros	
!
val handler = Macros.handler[Animal]	
}
Readers
val animalsFuture = 	
collection.	
find(query).	
cursor[Animal].	
collect[List]()	
!
Streaming
// Enumerator[BSONDocument]	
val enumeratorOfAnimals = 	
collection.	
find(query). 	
cursor[BSONDocument].	
enum...
Streaming
// Iteratee[BSONDocument, Unit]	
val process: = Iteratee.foreach { a =>	
val name = a[String]("name")	
val prett...
Streaming
enumeratorOfAnimals.apply(process)	
!
or:	
!
enumeratorOfAnimals |>>> process
Play
• Play json <-> Reactive mongo bson
• Async actions
• Iteratees
• GridFS
Play
object Application extends Controller 	
with MongoController {	
!
def collection: JSONCollection = 	
db.collection(“a...
Play
object JsonFormats {	
import play.api.libs.json.Json	
import play.api.data._	
import play.api.data.Forms._	
!
implici...
Play
def findByName(name: String) =	
Action.async {	
collection.	
find(Json.obj("name" -> name))	
cursor[JsObject].	
colle...
Play
def fromJson =	
Action.async(parse.json) { r =>	
r.body.validate[Animal].map { a =>	
collection.insert(a).map { … }	
...
Play
def upload =	
Action(gridFSBodyParser(gfs)) { r =>	
// Future[ReadFile[BSONValue]]	
val futureFile = r.body.files.hea...
Thanks!
• http://www.mongodb.org/
• http://reactivemongo.org/
• https://github.com/ReactiveMongo/ReactiveMongo/
• https://...
Upcoming SlideShare
Loading in …5
×

Talk about ReactiveMongo at MSUG May

700 views

Published on

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
700
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
6
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Talk about ReactiveMongo at MSUG May

  1. 1. Reactive Mongo Asynchronous driver for MongoDB
  2. 2. About me • My name is Andrey Neverov • “General rhetoric” • scala since 2011 • now: scala, akka, spray, mongodb • contribute to reactive mongo
  3. 3. Plan 1. What is reactive mongo 2. Connecting 3. Queries 4. Working with data 5. Streaming 6. Reactive Mongo + Play
  4. 4. Reactive Mongo • Asynchronous driver • Non-blocking I/O • Scalability • Streaming • GridFS
  5. 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. 6. Queries val query = BSONDocument( “age” -> BSONDocument( “$gt” -> 5 ) )
  7. 7. Queries val animalsOlderThanFive = collection. find(query). cursor[BSONDocument]. collect[List]() ! // Future[List[BSONDocument]]
  8. 8. Queries animalsOlderThanFive.map { case animals: List[Animal] => … } recover { case t: Throwable => // fail! }
  9. 9. Readers case class Animal(_id: BSONObjectID, age: Int) object Animal { implicit val reader = new BSONDocumentReader[Animal] { def read(doc: BSONDocument) = { Animal(…) }}}
  10. 10. Readers object Animal { import reactivemongo.bson.Macros ! val handler = Macros.handler[Animal] }
  11. 11. Readers val animalsFuture = collection. find(query). cursor[Animal]. collect[List]() !
  12. 12. Streaming // Enumerator[BSONDocument] val enumeratorOfAnimals = collection. find(query). cursor[BSONDocument]. enumerate()
  13. 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. 14. Streaming enumeratorOfAnimals.apply(process) ! or: ! enumeratorOfAnimals |>>> process
  15. 15. Play • Play json <-> Reactive mongo bson • Async actions • Iteratees • GridFS
  16. 16. Play object Application extends Controller with MongoController { ! def collection: JSONCollection = db.collection(“animals") }
  17. 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. 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. 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. 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. 21. Thanks! • http://www.mongodb.org/ • http://reactivemongo.org/ • https://github.com/ReactiveMongo/ReactiveMongo/ • https://github.com/neverov

×