secret
Last active

Set of examples of how to use ReactiveMongo with the Play-ReactiveMongo Plugin.

  • Download Gist
ComplaintController.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
def watchCollection = WebSocket.using[JsValue] { request =>
// Inserts the received messages into the capped collection
val in = Iteratee.flatten(futureCollection.map(collection => Iteratee.foreach[JsValue] { json =>
Logger.debug("received " + json)
collection.insert(json)
}))
 
// Enumerates the capped collection
val out = {
val futureEnumerator = futureCollection.map { collection =>
// so we are sure that the collection exists and is a capped one
val cursor: Cursor[JsValue] = collection
// we want all the documents
.find(Json.obj())
// the cursor must be tailable and await data
.options(QueryOpts().tailable.awaitData)
.cursor[JsValue]
 
// ok, let's enumerate it
cursor.enumerate
}
Enumerator.flatten(futureEnumerator)
}
 
// We're done!
(in, out)
}
GridFSController.scala
Scala
1 2 3 4 5 6 7 8 9 10
def getProfilePic(email : String) = Action {
Async {
users.find(Json.obj("email" -> email)).one[JsValue].filter(_.isDefined).flatMap {
maybeUser =>
val profileId = (maybeUser.get \ "profilePic" \ "_id" \ "$oid").as[String]
serve(gridFS, gridFS.find(BSONDocument("_id" -> new BSONObjectID(profileId))))
}
}
 
}
UserController.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13
def update(email : String) = Action(parse.json) {
request =>
Async {
users.update(findByEmail(email), request.body).map {
lastError =>
if (lastError.ok)
Ok(Json.obj("results" -> "success"))
else
BadRequest(Json.obj("error" -> s"Error occurred: ${lastError.errMsg.getOrElse("No details. Argh!")}"))
 
}
}
}
application.conf
1 2
# mongo settings
mongodb.uri="mongodb://reactive:<password>@paulo.mongohq.com:10065/reactive"
play.plugins
1
400:play.modules.reactivemongo.ReactiveMongoPlugin

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.