Skip to content

Instantly share code, notes, and snippets.

@tdrozdowski
Last active December 22, 2015 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdrozdowski/4451005fb1f31c730f75 to your computer and use it in GitHub Desktop.
Save tdrozdowski/4451005fb1f31c730f75 to your computer and use it in GitHub Desktop.
Set of examples of how to use ReactiveMongo with the Play-ReactiveMongo Plugin.
# mongo settings
mongodb.uri="mongodb://reactive:<password>@paulo.mongohq.com:10065/reactive"
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)
}
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))))
}
}
}
400:play.modules.reactivemongo.ReactiveMongoPlugin
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!")}"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment